Skip to content

Commit daabe58

Browse files
authored
fix: Log request body (#272)
1 parent 5052fd4 commit daabe58

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

client.go

+12-21
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,23 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
9797
if c.returnRepresentation {
9898
req.Header.Set("Prefer", "return=representation")
9999
}
100+
if c.Log != nil {
101+
if reqDump, err := httputil.DumpRequestOut(req, true); err == nil {
102+
c.Log.Write([]byte(fmt.Sprintf("Request: %s\n", reqDump)))
103+
}
104+
}
100105

101106
resp, err = c.Client.Do(req)
102-
c.log(req, resp)
103-
104107
if err != nil {
105108
return err
106109
}
110+
111+
if c.Log != nil {
112+
if respDump, err := httputil.DumpResponse(resp, true); err == nil {
113+
c.Log.Write([]byte(fmt.Sprintf("Response from %s: %s\n", req.URL, respDump)))
114+
}
115+
}
116+
107117
defer func(Body io.ReadCloser) error {
108118
return Body.Close()
109119
}(resp.Body)
@@ -180,22 +190,3 @@ func (c *Client) NewRequest(ctx context.Context, method, url string, payload int
180190
}
181191
return http.NewRequestWithContext(ctx, method, url, buf)
182192
}
183-
184-
// log will dump request and response to the log file
185-
func (c *Client) log(r *http.Request, resp *http.Response) {
186-
if c.Log != nil {
187-
var (
188-
reqDump string
189-
respDump []byte
190-
)
191-
192-
if r != nil {
193-
reqDump = fmt.Sprintf("%s %s. Data: %s", r.Method, r.URL.String(), r.Form.Encode())
194-
}
195-
if resp != nil {
196-
respDump, _ = httputil.DumpResponse(resp, true)
197-
}
198-
199-
c.Log.Write([]byte(fmt.Sprintf("Request: %s\nResponse: %s\n", reqDump, string(respDump))))
200-
}
201-
}

0 commit comments

Comments
 (0)