-5

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 \r\n. 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?

Lalle
  • 676
  • 12
  • 30
  • 2
    What does "dechunk the body from the http response, removing the \r\n" mean? Dechunking is not (at least not directly) related to removing Windows lineendings? – Volker Sep 12 '19 at 09:03
  • "it isn't removed automatically" -- what isn't removed? – Jonathan Hall Sep 12 '19 at 09:26
  • 4
    In any case, if you want help debugging your code, you _must_ include all relevant code in the question. Please update your question with a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Jonathan Hall Sep 12 '19 at 09:27
  • In https://golang.org/src/net/http/response.go at row 66, it says "The Body is automatically dechunked if the server replied with a "chunked" Transfer-Encoding.".. this does not happen. Why? That is my question. – Lalle Sep 12 '19 at 10:10
  • As already explained, until we can see your code, there's no possible way to tell you why it does anything. – Jonathan Hall Sep 12 '19 at 10:53
  • Updated now with new edit – Lalle Sep 13 '19 at 13:18

1 Answers1

0

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

Lalle
  • 676
  • 12
  • 30