Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang: default HTTP client doesn't handle compression

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading