Last active
December 10, 2018 12:26
-
-
Save juacompe/4f36cf9d024d479cf1ae07180fa4812c to your computer and use it in GitHub Desktop.
https with keep-alives history
This file contains 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 | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"net/http/httputil" | |
) | |
func main() { | |
url := "https://jsonplaceholder.typicode.com/posts/1" | |
req, _ := http.NewRequest("GET", url, nil) | |
req.Header.Add("cache-control", "no-cache") | |
req.Header.Set("Connection", "keep-alive") | |
tr := &http.Transport{ | |
DisableKeepAlives: false, | |
} | |
http.DefaultClient.Transport = tr | |
for i := 0; i < 2; i++ { | |
res, _ := http.DefaultClient.Do(req) | |
b, _ := httputil.DumpResponse(res, true) | |
fmt.Println(string(b)) | |
defer dumpRemainingResponse(res) | |
body, _ := ioutil.ReadAll(res.Body) | |
fmt.Println(res) | |
fmt.Println(string(body)) | |
fmt.Println(res) | |
fmt.Println(string(body)) | |
} | |
} | |
func dumpRemainingResponse(res *http.Response) { | |
if res != nil { | |
io.Copy(ioutil.Discard, res.Body) | |
res.Body.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment