diff options
author | William Carroll <wpcarro@gmail.com> | 2020-02-09T17·49+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-02-10T10·06+0000 |
commit | 4ea5a1bffad0628db593b7158216113c7c1956bd (patch) | |
tree | 67f17089e0741a51cc954d66ec0d89cea4854176 /gopkgs/utils | |
parent | 81271498b6c8c765ed114b8ed92b1f9f6f08a406 (diff) |
Support utils.Debug{Request,Response}
Exposing functions to print HTTP request and response structs.
Diffstat (limited to 'gopkgs/utils')
-rw-r--r-- | gopkgs/utils/utils.go | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/gopkgs/utils/utils.go b/gopkgs/utils/utils.go index 24a01cea9d1d..404fd8bf0a12 100644 --- a/gopkgs/utils/utils.go +++ b/gopkgs/utils/utils.go @@ -2,8 +2,9 @@ package utils import ( - "log" + "fmt" "io/ioutil" + "log" "net/http" "net/http/httputil" ) @@ -15,6 +16,18 @@ func FailOn(err error) { } } +// Prints the verbose form of an HTTP request. +func DebugRequest(req *http.Request) { + bytes, _ := httputil.DumpRequest(req, true) + fmt.Println(string(bytes)) +} + +// Prints out the verbose form of an HTTP response. +func DebugResponse(res *http.Response) { + bytes, _ := httputil.DumpResponse(res, true) + fmt.Println(string(bytes)) +} + // Make a simple GET request to `url`. Fail if anything returns an error. I'd // like to accumulate a library of these, so that I can write scrappy Go // quickly. For now, this function just returns the body of the response back as @@ -36,10 +49,8 @@ func SimpleGet(url string, headers map[string]string, debug bool) string { defer res.Body.Close() if debug { - bytes, _ := httputil.DumpRequest(req, true) - log.Println(string(bytes)) - bytes, _ = httputil.DumpResponse(res, true) - log.Println(string(bytes)) + DebugRequest(req) + DebugResponse(res) } if res.StatusCode == http.StatusOK { |