duanche5149 2015-08-07 16:11
浏览 149
已采纳

Fiddler环绕JSON响应

I have a web service implemented in Go that returns a JSON structure from an external service. Upon returning the object, it looks like this:

{"otherServiceInfoList":[],"action...

My Go web service simply reads the JSON to a slice:

response, err := ioutil.ReadAll(resp.Body)

and returns it to the client:

w.Write(response)

The response is displayed as-is in Postman, however Fiddler both prepends and appends the response as follows:

34ee
{"otherServiceInfoList":[],"...
0

Note the leading 34ee and trailing 0.

I am then promoted to transform the response:

"Response is encoded and may require decoding before inspection."

Accepting the prompt removes returns the original JSON. Is Go's w.write method applying the extra characters, or is this specific to Fiddler?

Incidentally, I'm setting the following header before writing to the buffer:

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  • 写回答

2条回答 默认 最新

  • dpgjci27392 2015-08-07 21:22
    关注

    You're dealing with a chunked response. I'm not sure what your end goal is but there are a few different options. The source itself says;

        // Body represents the response body.
        //
        // The http Client and Transport guarantee that Body is always
        // non-nil, even on responses without a body or responses with
        // a zero-length body. It is the caller's responsibility to
        // close Body.
        //
        // The Body is automatically dechunked if the server replied
        // with a "chunked" Transfer-Encoding.
        Body io.ReadCloser
    

    So for example here; response, err := ioutil.ReadAll(resp.Body) where you're relaying the response from the other service, you could fix the problem by making the service that provided resp set a Transfer-Encoding header with the value chunked, assuming you have access to that api as well. If you're only working in this middle layer, then you'll have to dechunk the response yourself before writing it. If the request you're monitoring in Fiddler doesn't have chunked Transfer-Encoding, just adding that may cause Fiddler to display it the same as you see it in Postman.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度