Skip to content

Instantly share code, notes, and snippets.

@falzm
Created March 5, 2020 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falzm/994cb921747137d20132a9589e66e8f7 to your computer and use it in GitHub Desktop.
Save falzm/994cb921747137d20132a9589e66e8f7 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"net/http"
"net/http/httputil"
"os"
)
type DebugTransport struct {
current *http.Request
}
func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var scanner *bufio.Scanner
t.current = req
resp, err := http.DefaultTransport.RoundTrip(req)
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
// Don't bother handling dumping error, it's not vital to the request workflow
return resp, err
}
scanner = bufio.NewScanner(bytes.NewBuffer(dump))
for scanner.Scan() {
fmt.Fprintf(os.Stderr, "> %s\n", scanner.Text())
}
if err := scanner.Err(); err != nil {
return resp, err
}
fmt.Fprintln(os.Stderr, "---")
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
// Don't bother handling dumping error, it's not vital to the request workflow
return resp, err
}
scanner = bufio.NewScanner(bytes.NewBuffer(dump))
for scanner.Scan() {
fmt.Fprintf(os.Stderr, "< %s\n", scanner.Text())
}
if err := scanner.Err(); err != nil {
return resp, err
}
return resp, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment