Add logging round tripper:
import (
"fmt"
"net/http"
"net/http/httputil"
)
// This type implements the http.RoundTripper interface.
type LoggingRoundTripper struct {
Proxied http.RoundTripper
}
func (lrt LoggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
// Log request
dumpReq, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Printf("ERROR dumping request: %v", err)
return
}
fmt.Printf("REQUEST:\n%s\n", dumpReq)
// Send the request, get the response (or the error)
res, err = lrt.Proxied.RoundTrip(req)
if err != nil {
fmt.Printf("ERROR: %v", err)
return
}
// Log response
dumpResp, err := httputil.DumpResponse(res, true)
if err != nil {
fmt.Printf("ERROR dumping response: %v", err)
return
}
fmt.Printf("RESULT:\n%s\n", dumpResp)
return
}
And then you can use it like:
httpClient := &http.Client{Transport: LoggingRoundTripper{http.DefaultTransport}}