Fiddler 环绕 JSON 响应

Fiddler Surrounds JSON Response

我有一个用 Go 实现的 Web 服务,returns 来自外部服务的 JSON 结构。返回 object 后,它看起来像这样:

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

我的 Go web 服务只是将 JSON 读取为一个片段:

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

和returns它给客户:

w.Write(response)

响应在 Postman 中显示 as-is,但是 Fiddler 会按如下方式预先添加和附加响应:

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

注意前导 34ee 和尾随 0

我再提升改造回复:

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

接受提示会删除 returns 原来的 JSON。 Go 的 w.write 方法是否应用了额外的字符,或者这是特定于 Fiddler 的?

顺便说一下,我在写入缓冲区之前设置了以下 header:

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

这是 http 1.1 分块响应。协议将发送格式:

size-of-chunk-in-hex
chunk
...

最终块大小为 0 表示响应结束。您的示例显示响应为 13550 字节,并以一个块的形式发送。

您正在处理分块响应。我不确定您的最终目标是什么,但有几种不同的选择。消息来源本身说;

    // 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

例如这里; response, err := ioutil.ReadAll(resp.Body) 在您转发来自其他服务的响应的地方,您可以通过使提供 resp 的服务设置 Transfer-Encoding header 值分块来解决问题,假设您也可以访问 api。如果您只在这个中间层工作,那么您必须在编写之前自行分解响应。如果您在 Fiddler 中监视的请求没有 chunked Transfer-Encoding,只需添加它可能会导致 Fiddler 显示它与您在 Postman 中看到的一样。