DumpRequest pretty prints an http.Request
(req *http.Request)
| 51 | |
| 52 | // DumpRequest pretty prints an http.Request |
| 53 | func 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 | |
| 79 | func sortedHeaderKeys(req *http.Request) []string { |
| 80 | var keys []string |