MCPcopy
hub / github.com/qustavo/httplab / DumpRequest

Function DumpRequest

dump.go:53–77  ·  view source on GitHub ↗

DumpRequest pretty prints an http.Request

(req *http.Request)

Source from the content-addressed store, hash-verified

51
52// DumpRequest pretty prints an http.Request
53func DumpRequest(req *http.Request) ([]byte, error) {
54 buf := bytes.NewBuffer(nil)
55
56 reqURI := req.RequestURI
57 if reqURI == "" {
58 reqURI = req.URL.RequestURI()
59 }
60
61 fmt.Fprintf(buf, "%s %s %s/%d.%d\n",
62 withColor(35, valueOrDefault(req.Method, "GET")),
63 reqURI,
64 withColor(35, "HTTP"),
65 req.ProtoMajor,
66 req.ProtoMinor,
67 )
68
69 keys := sortedHeaderKeys(req)
70 for _, key := range keys {
71 val := req.Header.Get(key)
72 fmt.Fprintf(buf, "%s: %s\n", withColor(31, key), withColor(32, val))
73 }
74
75 err := writeBody(buf, req)
76 return buf.Bytes(), err
77}
78
79func sortedHeaderKeys(req *http.Request) []string {
80 var keys []string

Callers 3

AddRequestMethod · 0.92
TestDumpRequestWithJSONFunction · 0.85
TestDumpRequestHeadersFunction · 0.85

Calls 5

withColorFunction · 0.85
valueOrDefaultFunction · 0.85
sortedHeaderKeysFunction · 0.85
writeBodyFunction · 0.85
GetMethod · 0.45

Tested by 2

TestDumpRequestWithJSONFunction · 0.68
TestDumpRequestHeadersFunction · 0.68