Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created March 16, 2012 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elazarl/2050445 to your computer and use it in GitHub Desktop.
Save elazarl/2050445 to your computer and use it in GitHub Desktop.
Client.Do will not uncompress response body
package main
// run this file with `go run clientDoGzip.go, and then file tmp.out to verify body is gzipped
import "net/http"
import "io/ioutil"
import "bytes"
import "fmt"
import "log"
import "compress/gzip"
func main() {
req,err := http.NewRequest("GET","http://bash.cyberciti.biz/wp-content/themes/bashshell/custom/images/headerbg.gif",nil)
if err != nil {log.Fatal(err)}
req.Header.Set("Accept-Encoding","gzip")
if err != nil {log.Fatal(err)}
client := &http.Client{}
resp,err := client.Do(req)
if err != nil {log.Fatal(err)}
for k,v := range resp.Header {
fmt.Println(k+":",v)
}
b,err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
r,err := gzip.NewReader(bytes.NewReader(b))
if err == nil {
log.Println("Hold it, resp.Body should be plain text!")
if _,err := r.Read([]byte{0}); err == nil {
log.Println("And we can even read the first byte!")
}
}
ioutil.WriteFile("tmp.out",b,0666)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment