Missing Content-Length in http responses

2,166 views
Skip to first unread message

daniel.rob...@gmail.com

unread,
Apr 9, 2016, 5:57:22 AM4/9/16
to golang-nuts
Im making a http request using http.DefaultClient.Do(req), The request is a GET to download a large file, the response is chunked and it should contain a Content-Length header but by the time I get the response object the Header property has no Content-Length and the ContentLength property is -1, does the go http package strip out the Content-Length header and then because its a chunked response unable to workout what the actual body length is so assigns ContentLength as -1? This is the only explanation I can think of as to why I can't get the Content-Length value for my file download requests.

Thanks,

Dan 

Tamás Gulácsi

unread,
Apr 9, 2016, 6:30:50 AM4/9/16
to golang-nuts
Chunked encoding opposes content-length: if the length is known, nő need to chunk the response; if the response is chunked, when should it end? When the closing 0 comes, or when has read content-length bytes???

daniel.rob...@gmail.com

unread,
Apr 9, 2016, 7:25:59 AM4/9/16
to golang-nuts
Thanks, I have found that the defaultClient adds Accepts Encoding gzip by default to the request which was preventing the response returning a Content Length if I use a new client with:


client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
DisableCompression: true,
}}

The DisableCompression: true stops it from adding the Accepts Encoding header and so I do get back a Content Length now
Reply all
Reply to author
Forward
0 new messages