Skip to content

Instantly share code, notes, and snippets.

@juacompe
Last active December 10, 2018 12:26
Show Gist options
  • Save juacompe/4f36cf9d024d479cf1ae07180fa4812c to your computer and use it in GitHub Desktop.
Save juacompe/4f36cf9d024d479cf1ae07180fa4812c to your computer and use it in GitHub Desktop.
https with keep-alives history
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