Skip to content

Commit 4c4fa9f

Browse files
author
Marcel Edmund Franke
authored
Merge pull request #42 from donutloop/feature/http_pretty_request_dump
add pretty print for http request
2 parents 5508979 + 0f94114 commit 4c4fa9f

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

debugutil/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,28 @@ func main() {
3838
}
3939
log.Println(s)
4040
}
41+
```
42+
43+
PrettyRequestDump creates a human readable representation of the value http.Request.
44+
45+
## Example
46+
47+
```go
48+
package main
49+
50+
import (
51+
"github.com/donutloop/toolkit/debugutil"
52+
"log"
53+
"net/http"
54+
)
55+
56+
func main() {
57+
58+
req := &http.Request{}
59+
s , err := debugutil.PrettySprintRequest(req)
60+
if err != nil {
61+
log.Fatal(err)
62+
}
63+
log.Println(s)
64+
}
4165
```

debugutil/http_dump.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func PrettySprintResponse(resp *http.Response) (string, error) {
1818
return string(dump), nil
1919
}
2020

21-
// PrettyDumpResponse is like DumpResponse but dump is pretty formatted.
21+
// PrettyDumpResponse is like httputil.DumpResponse but dump is pretty formatted.
2222
func PrettyDumpResponse(resp *http.Response, body bool) ([]byte, error) {
2323

2424
b, err := httputil.DumpResponse(resp, body)
@@ -38,5 +38,37 @@ func PrettyDumpResponse(resp *http.Response, body bool) ([]byte, error) {
3838
return buffer.Bytes(), nil
3939
}
4040

41+
return b, nil
42+
}
43+
44+
// PrettySprintRequest is pretty printing a http request
45+
func PrettySprintRequest(resp *http.Request) (string, error) {
46+
dump, err := PrettyDumpRequest(resp, true)
47+
if err != nil {
48+
return "", err
49+
}
50+
return string(dump), nil
51+
}
52+
53+
// PrettyDumpRequest is like httputil.DumpRequest but dump is pretty formatted.
54+
func PrettyDumpRequest(req *http.Request, body bool) ([]byte, error) {
55+
56+
b, err := httputil.DumpRequest(req, body)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
header := req.Header.Get("Content-type")
62+
if body && strings.Contains(header, "application/json") && req.ContentLength > 0 {
63+
buffer := new(bytes.Buffer)
64+
jsonRaw := b[int64(len(b))-req.ContentLength:]
65+
b = b[:int64(len(b))-req.ContentLength]
66+
buffer.Write(b)
67+
if err := json.Indent(buffer, jsonRaw, "", "\t"); err != nil {
68+
return nil, err
69+
}
70+
return buffer.Bytes(), nil
71+
}
72+
4173
return b, nil
4274
}

debugutil/http_dump_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,28 @@ X-Xss-Protection: 1; mode=block
4747

4848
t.Log(s)
4949
}
50+
51+
func TestPrettyDumpRequest(t *testing.T) {
52+
53+
b := []byte(`{"data": {"partner": {"verified_data": {"people": [{"identity_document_checks": [{"status": "passed", "file_links": [{"file_type": "photo", "link": "https://static.iwoca.com/assets/iwoca.4c17fef7de62.png"}], "check_id": "REFERENCE_0001", "datetime": "2017-06-12T14:05:51.666Z", "identity_document_type": "passport", "document_issuing_country": "gb", "provider_name": "test"}], "uid": "6cf7319e-f9ec-4038-ba4f-3561a6097484"}]}}, "state_key": "8e4db383-2ead-4a47-87df-220432714c47", "schema_version": "v1", "application": {"company": {"last_12_months_turnover": {"amount": 700000, "datetime": "2016-10-12T14:05:51.666Z"}, "type": "gmbh", "company_number": "01111112", "bank_details": {"iban": "DE89370400440532013000"}}, "requested_products": {"credit_facility": {"approval": {"amount": 15000}}}, "people": [{"residential_addresses": [{"town": "Ely", "uid": "cf9aa203-4e0c-4d7f-b42b-90c7b3d193d3", "house_number": "286", "date_from": "2014-02-03", "street_line_1": "Idverifier St", "postcode": "CB62AG"}], "last_name": "Norton", "uid": "6cf7319e-f9ec-4038-ba4f-3561a6097484", "roles": ["applicant", "shareholder", "guarantor", "director"], "title": "herr", "first_name": "Ervin", "privacy_policy": {"agreed": true, "datetime": "2016-10-12T14:05:51.666Z"}, "date_of_birth": "1980-01-01"}]}}}`)
54+
55+
req, err := http.NewRequest(http.MethodGet, "/api/resource", bytes.NewReader(b))
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
req.Header.Set("Content-Type", "application/json")
61+
62+
req.ContentLength = int64(len(b))
63+
64+
if req.ContentLength != 1288 {
65+
t.Logf("content length of request body is bad, %v", req.ContentLength)
66+
}
67+
68+
s, err := debugutil.PrettySprintRequest(req)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
73+
t.Log(s)
74+
}

0 commit comments

Comments
 (0)