Created
March 5, 2020 07:53
-
-
Save falzm/994cb921747137d20132a9589e66e8f7 to your computer and use it in GitHub Desktop.
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 ( | |
"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