Skip to content

Instantly share code, notes, and snippets.

@skyend
Created February 24, 2021 16:07
Show Gist options
  • Select an option

  • Save skyend/f6765cf0f88de7e946a2e6edb0720157 to your computer and use it in GitHub Desktop.

Select an option

Save skyend/f6765cf0f88de7e946a2e6edb0720157 to your computer and use it in GitHub Desktop.
Golang decompress gzip of response came from http.request
func Request() {
client := &http.Client{
//CheckRedirect: redirect
Transport: &http.Transport{DisableCompression: false},
}
req, err := http.NewRequest("GET","https://jsonplaceholder.typicode.com/todos/1", nil)
req.Header.Add("Accept-Encoding", "gzip, deflate")
if err != nil {
fmt.Println("Failed to create new request.")
}
resp, err :=client.Do(req)
if err != nil {
fmt.Println("Failed to request.")
}
defer resp.Body.Close()
reader,err:=gzip.NewReader(resp.Body)
body, err := io.ReadAll(reader)
s:= string(body)
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment