Created
March 16, 2012 15:01
-
-
Save elazarl/2050445 to your computer and use it in GitHub Desktop.
Client.Do will not uncompress response body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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