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 package utils
import ( import (
"log" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/http/httputil" "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 // 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 // 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 // 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() defer res.Body.Close()
if debug { if debug {
bytes, _ := httputil.DumpRequest(req, true) DebugRequest(req)
log.Println(string(bytes)) DebugResponse(res)
bytes, _ = httputil.DumpResponse(res, true)
log.Println(string(bytes))
} }
if res.StatusCode == http.StatusOK { if res.StatusCode == http.StatusOK {