Created
February 24, 2021 16:07
-
-
Save skyend/f6765cf0f88de7e946a2e6edb0720157 to your computer and use it in GitHub Desktop.
Golang decompress gzip of response came from http.request
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
| 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