Skip to content

Commit 4c70274

Browse files
authored
feat: add Dump option for easy debugging (#26)
* feat: add Dump option for easy debugging * feat: add dump example to README.md
1 parent c6b1607 commit 4c70274

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,11 @@ r, err := requests.Get("http://example.com?a=1&b=2",
232232
if err != nil { /* ... */ }
233233
// URL: http://example.com?a=1&b=2&c=3
234234
```
235+
236+
### Dump outgoing client request and response
237+
238+
```go
239+
var reqDump, respDump string
240+
r, err := requests.Get("http://example.com",
241+
requests.Dump(&request, &respDump))
242+
```

options.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type httpOptions struct {
3030
Timeout time.Duration
3131

3232
DisableKeepAlives bool
33+
// dump
34+
DumpRequestOut *string
35+
DumpResponse *string
3336
}
3437

3538
// Option is the functional option type.
@@ -207,6 +210,19 @@ func DisableKeepAlives() Option {
207210
}
208211
}
209212

213+
// Dump dumps outgoing client request and response to the corresponding
214+
// input param (req or resp) if not nil.
215+
//
216+
// Refer:
217+
// - https://pkg.go.dev/net/http/httputil#DumpRequestOut
218+
// - https://pkg.go.dev/net/http/httputil#DumpResponse
219+
func Dump(req, resp *string) Option {
220+
return func(opts *httpOptions) {
221+
opts.DumpRequestOut = req
222+
opts.DumpResponse = resp
223+
}
224+
}
225+
210226
// newDefaultOptions creates a new default HTTP options.
211227
func newDefaultOptions() *httpOptions {
212228
return &httpOptions{

request.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"mime/multipart"
1212
"net/http"
13+
"net/http/httputil"
1314
"net/url"
1415
"strings"
1516

@@ -100,12 +101,29 @@ func request(method, urlStr string, options ...Option) (*Response, error) {
100101
Transport: transport,
101102
}
102103

104+
if opts.DumpRequestOut != nil {
105+
reqDump, err := httputil.DumpRequestOut(req, true)
106+
if err != nil {
107+
return nil, err
108+
}
109+
*opts.DumpRequestOut = string(reqDump)
110+
}
111+
103112
// If the returned error is nil, the Response will contain
104113
// a non-nil Body which the user is expected to close.
105114
resp, err := client.Do(req)
106115
if err != nil {
107116
return nil, err
108117
}
118+
119+
if opts.DumpResponse != nil {
120+
respDump, err := httputil.DumpResponse(resp, true)
121+
if err != nil {
122+
return nil, err
123+
}
124+
*opts.DumpResponse = string(respDump)
125+
}
126+
109127
return newResponse(resp, opts)
110128
}
111129

request_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func TestPostJson(t *testing.T) {
339339

340340
var jsonResp EchoResponse
341341
var textResp string
342+
var reqDump, respDump string
342343
type args struct {
343344
url string
344345
options []Option
@@ -363,6 +364,7 @@ func TestPostJson(t *testing.T) {
363364
JSON(&EchoRequest{ID: 1, Name: "Hello"}),
364365
ToJSON(&jsonResp),
365366
ToText(&textResp),
367+
Dump(&reqDump, &respDump),
366368
},
367369
timeout: 5 * time.Second,
368370
},
@@ -386,6 +388,8 @@ func TestPostJson(t *testing.T) {
386388
t.Logf("body: %+v", got.Text())
387389
t.Logf("body(text): %+v", textResp)
388390
t.Logf("body(json): %+v", jsonResp)
391+
t.Logf("Request(dump):\n%s", reqDump)
392+
t.Logf("Response(dump):\n%s", respDump)
389393
} else {
390394
t.Logf("Get failed: %v", err)
391395
}

0 commit comments

Comments
 (0)