Get the raw text of a request.

polaris · · 813 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Here is my problem i&#39;am debugging my app, i have to receive some json from one of my server.</p> <p>It works fine and i map the json to a struct.</p> <p>but for curiosity an debugging purpose i would like to see the raw json receive by my golang server.</p> <p>here is my code :</p> <pre><code>func put_id(w http.ResponseWriter, r *http.Request) { var data_send return_data data_send.Code = 200 data_json, err := json.Marshal(data_send) if (err != nil){ Info.Println(&#34;error&#34;) Info.Println(err) } decoder := json.NewDecoder(r.Body) var t img err2 := decoder.Decode(&amp;t) if (err2 != nil){ Info.Println(&#34;error&#34;) Info.Println(err2) } var body struct { // httpbin.org sends back key/value pairs, no map[string][]string Headers map[string]string `json:&#34;headers&#34;` Origin string `json:&#34;origin&#34;` } err3 := decoder.Decode(&amp;body) if (err3 != nil){ Info.Println(&#34;error&#34;) Info.Println(err3) } body2, err4 := ioutil.ReadAll(r.Body) if err4 != nil { log.Fatalf(&#34;ERROR: %s&#34;, err4) } Info.Println(body2) Info.Println(t.IdMongo) Info.Println(t.Name) Info.Println(t.IdMongoImg) Info.Println(body) var ti img_pic ti.IdMongo = t.IdMongoImg ti.Name = t.Name ti.Type = t.Type ti.Img_base64 = t.Img_base64 ti.ImgId = t.ImgId t.Img_base64 = &#34;&#34; recordImgMongo(t,ti) recordImgRedis(t) w.Header().Set(&#34;Content-Type&#34;, &#34;application/json&#34;) w.Header().Set(&#34;Content-Length&#34;, strconv.Itoa(len(data_json))) //len(dec) w.Write(data_json) } </code></pre> <p>Everything works fine but for curiosity i would like to see the raw json as text.</p> <p>I&#39;ve tried some things but none works (as body and body2 show)</p> <p>Thanks and regards</p> <hr/>**评论:**<br/><br/>sairamk: <pre><p>You can use <a href="https://godoc.org/net/http/httputil#DumpRequest" rel="nofollow">DumpRequest</a> from <a href="https://godoc.org/net/http/httputil" rel="nofollow">net/http/httputil</a> package.</p> <p>This gives both the body and headers. </p> <p>Reads the body from the request if not read.</p> <blockquote> <p>body2, err4 := ioutil.ReadAll(r.Body)</p> </blockquote></pre>bussiere: <pre><p>thanks but sorry i don&#39;t understand your answer ...</p></pre>j_d_q: <pre><p>The standard library has a built in helper for this</p> <pre><code>byts, _ := httputil.DumpRequest(r, true) fmt.Println(string(byts)) </code></pre> <p>Look at the <a href="https://golang.org/pkg/net/http/httputil/#DumpRequest" rel="nofollow">DumpRequest docs</a>, they&#39;re pretty well documented</p> <p>Edit: he linked both. Those two links will output everything visible about the request</p></pre>bussiere: <pre><p>thanks</p></pre>teepark: <pre><p>Your problem in the code above is that <code>decoder.Decode()</code> will consume the entire request body, then it won&#39;t be available to anything else afterwards. That&#39;s why <code>body</code> and <code>body2</code> get nothing.</p> <p>The cleanest solution is to use a <a href="https://godoc.org/io#TeeReader" rel="nofollow">TeeReader</a> to wrap <code>r.Body</code> (as the reader) and a new <code>bytes.Buffer</code> (as the writer), then decode from that. Now you should have the raw bytes in the buffer, having only consumed the request body once since the TeeReader handled saving it off at the same time.</p></pre>bussiere: <pre><p>thanks</p></pre>SilentWeaponQuietWar: <pre><p>I prefer the go-spew package for getting easy to read output</p> <p><a href="https://github.com/davecgh/go-spew" rel="nofollow">https://github.com/davecgh/go-spew</a></p> <blockquote> <p>spew.Dump(data_json)</p> </blockquote></pre>bussiere: <pre><p>thanks</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

813 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传