【问题标题】:Fiddler Surrounds JSON ResponseFiddler 包围 JSON 响应
【发布时间】:2015-08-07 16:11:07
【问题描述】:

我在 Go 中实现了一个 Web 服务,它从外部服务返回一个 JSON 结构。返回对象后,它看起来像这样:

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

我的 Go Web 服务只是将 JSON 读取到一个切片中:

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

并将其返回给客户端:

w.Write(response)

响应在 Postman 中按原样显示,但 Fiddler 会按如下方式预先和附加响应:

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

注意前面的34ee 和后面的0

然后我被提升为转换响应:

“响应已编码,可能需要在检查前解码。”

接受提示删除返回原始 JSON。是 Go 的 w.write 方法应用了额外的字符,还是 Fiddler 特有的?

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

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

【问题讨论】:

  • 重启程序后,JSON 对象前面会加上34f7
  • 你能发布一个完整的、最小的例子来重现这种行为吗?
  • 看起来您看到的是 HTTP 分块响应
  • 知道如何在 Spring-Boot RESTful 应用程序中删除它吗?

标签: json go fiddler


【解决方案1】:

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

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

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

【讨论】:

  • \r\n\r\n size-of-chunk-in-hex \r\n 。前一个是标准东西的尾巴
【解决方案2】:

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

    // 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 标头来解决问题,假设您也可以访问该 api。如果您只在这个中间层工作,那么您必须在编写响应之前自己对响应进行分解。如果您在 Fiddler 中监控的请求没有 chunked Transfer-Encoding,则只需添加它可能会导致 Fiddler 显示与您在 Postman 中看到的相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2022-01-14
    • 2011-09-01
    • 1970-01-01
    • 2014-10-07
    相关资源
    最近更新 更多