-
-
Save willis7/c0428127b4877e8fecf11c4c30083556 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 "compress/gzip" | |
import "net/http" | |
import "fmt" | |
import "io" | |
import "log" | |
import "os" | |
func main() { | |
url := "http://bash.cyberciti.biz/wp-content/themes/bashshell/custom/images/headerbg.gif" | |
req, err := http.NewRequest("GET", url, 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) | |
} | |
defer resp.Body.Close() | |
for k, v := range resp.Header { | |
fmt.Println(k+":", v) | |
} | |
var r io.Reader | |
switch req.Header.Get("Accept-Encoding") { | |
case "gzip": | |
r, err = gzip.NewReader(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
default: | |
r = resp.Body | |
} | |
fd, err := os.Create("tmp.out") | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = io.Copy(fd, r) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment