2019-09-12 08:56
浏览 102


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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyoucha0645 2019-09-18 11:27

    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.


    点赞 打赏 评论