Skip to content

Instantly share code, notes, and snippets.

@rafaelsq
Last active August 13, 2020 19:15
Show Gist options
  • Save rafaelsq/b790c91a9e422e60c036e3cdd06c4d97 to your computer and use it in GitHub Desktop.
Save rafaelsq/b790c91a9e422e60c036e3cdd06c4d97 to your computer and use it in GitHub Desktop.
Print Request
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
)
type DumpTransport struct {
r http.RoundTripper
}
func (d *DumpTransport) RoundTrip(h *http.Request) (*http.Response, error) {
dump, _ := httputil.DumpRequestOut(h, true)
os.Stdout.WriteString("\nREQUEST===========================\n")
os.Stdout.Write(dump)
os.Stdout.WriteString(".end--------------------------------\n")
resp, err := d.r.RoundTrip(h)
dump, _ = httputil.DumpResponse(resp, true)
os.Stdout.WriteString("\nRESPONSE==========================\n")
os.Stdout.Write(dump)
os.Stdout.WriteString(".end--------------------------------\n")
return resp, err
}
func main() {
client := http.Client{
Transport: &DumpTransport{http.DefaultTransport},
}
resp, err := client.Get("https://gist.github.com/rafaelsq/b790c91a9e422e60c036e3cdd06c4d97")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment