转到http客户端不会自动将主体脱壳

I'm streaming http from Go and the server responds with "Transfer-Encoding: chunked" as expected. And I've been told that the http client in Go shall automatically dechunk the body from the http response, removing the . But in my case it isn't removed automatically, so I have to use a ChunkedReader to read the bodies.

Any idea why golang doesn't dechunk my body automatically?

EDIT: Here is the http request:

var transport = http.Transport{
  Proxy:                  nil,
  ExpectContinueTimeout:  0,
  MaxResponseHeaderBytes: 16384}

var httpClient = http.Client{
  Transport: &transport,
  CheckRedirect: func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
}}

bodyReader, bodyWriter := io.Pipe()
req, _ := http.NewRequest("GET", "http://x.x.x.x/stream", bodyReader)
response, err := httpClient.Do(req)

buffer := make([]byte, 2 << 15)
n, readErr = response.Body.Read(buffer)   <-- should be dechunked body

The data read into the buffer is not dechunked. Any idea why?

doukao8851
doukao8851 现在更新了新的编辑
10 个月之前 回复
douyi1963
douyi1963 如前所述,在看不到您的代码之前,没有任何方法可以告诉您它为什么执行任何操作。
10 个月之前 回复
douyi1084
douyi1084 在golang.org/src/net/http/response.go的第66行中,它说:“如果服务器回复了“分块”的Transfer-Encoding,则主体会自动分块。”..这不会发生。为什么?那是我的问题。
10 个月之前 回复
douche3791
douche3791 无论如何,如果需要帮助调试代码,则必须在问题中包括所有相关代码。请使用最小的,可复制的示例来更新您的问题。
10 个月之前 回复
duanjiong5023
duanjiong5023 “不会自动删除”-什么不会删除?
10 个月之前 回复
duanhemou9834
duanhemou9834 “从http响应中删除正文,删除 ”是什么意思?Dechunking与(至少不直接)与删除Windows行尾无关吗?
10 个月之前 回复

1个回答



我想出了为什么不自动对主体进行分块的原因。 这是因为HTTP响应是HTTP / 1.0。 在这种情况下,golang会忽略传输编码标头。</ p>

https ://github.com/golang/go/issues/12785 </ p>
</ div>

展开原文

原文

I figured out why the body is not dechunked automatically. It's because the HTTP response was HTTP/1.0. In which case golang ignores the transfer encoding header.

https://github.com/golang/go/issues/12785

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问