Support utils.Debug{Request,Response}

Exposing functions to print HTTP request and response structs.
This commit is contained in:
William Carroll 2020-02-09 17:49:06 +00:00
parent 81271498b6
commit 4ea5a1bffa

View file

@ -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 {