I’m trying to do an HTTP request and have golang add accept-encoding header and decompress the response automatically if it is compressed. I was under the impression that the default HTTP client should handle it transparently? However it doesn’t appear to:
req, _ := http.NewRequest("Get", "https://stackoverflow.com", nil)
// req.Header.Add("Accept-Encoding", "gzip")
client := &http.Client{}
resp, _ := client.Do(req)
println(resp.Header.Get("Content-Encoding"))
If I add the Accept-Encoding manually, it sends the header, but I have to uncompress the response manually.
>Solution :
If the Transport requests gzip on its own and gets a gzipped response, then the response is transparently decompressed. The transport removes the Content-Encoding header from the response in this case.
Check the Response. Uncompressed field to determine if the response was uncompressed.
req, _ := http.NewRequest("Get", "https://stackoverflow.com", nil)
resp, _ := http.DefaultClient.Do(req)
fmt.Println(resp.Uncompressed) // prints true
fmt.Println(resp.Header.Get("Content-Encoding")) // prints blank line
If the application explicitly requests gzip, then the response is returned as is.
req, _ := http.NewRequest("Get", "https://stackoverflow.com", nil)
req.Header.Add("Accept-Encoding", "gzip")
client := &http.Client{}
resp, _ := client.Do(req)
fmt.Println(resp.Uncompressed) // prints false
fmt.Println(resp.Header.Get("Content-Encoding")) prints gzip