如果传输本身请求gzip并获得gzipped响应,则该响应将被透明地解压缩。在本例中,传输从响应中删除Content-Encoding头。检查回复。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

如果应用程序显式请求gzip,那么响应将按原样返回。

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