mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 02:11:02 +01:00
fix: Log request body (#272)
This commit is contained in:
parent
5052fd4286
commit
daabe58f88
33
client.go
33
client.go
|
@ -97,13 +97,23 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
if c.returnRepresentation {
|
if c.returnRepresentation {
|
||||||
req.Header.Set("Prefer", "return=representation")
|
req.Header.Set("Prefer", "return=representation")
|
||||||
}
|
}
|
||||||
|
if c.Log != nil {
|
||||||
|
if reqDump, err := httputil.DumpRequestOut(req, true); err == nil {
|
||||||
|
c.Log.Write([]byte(fmt.Sprintf("Request: %s\n", reqDump)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp, err = c.Client.Do(req)
|
resp, err = c.Client.Do(req)
|
||||||
c.log(req, resp)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Log != nil {
|
||||||
|
if respDump, err := httputil.DumpResponse(resp, true); err == nil {
|
||||||
|
c.Log.Write([]byte(fmt.Sprintf("Response from %s: %s\n", req.URL, respDump)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
defer func(Body io.ReadCloser) error {
|
defer func(Body io.ReadCloser) error {
|
||||||
return Body.Close()
|
return Body.Close()
|
||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
|
@ -180,22 +190,3 @@ func (c *Client) NewRequest(ctx context.Context, method, url string, payload int
|
||||||
}
|
}
|
||||||
return http.NewRequestWithContext(ctx, method, url, buf)
|
return http.NewRequestWithContext(ctx, method, url, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// log will dump request and response to the log file
|
|
||||||
func (c *Client) log(r *http.Request, resp *http.Response) {
|
|
||||||
if c.Log != nil {
|
|
||||||
var (
|
|
||||||
reqDump string
|
|
||||||
respDump []byte
|
|
||||||
)
|
|
||||||
|
|
||||||
if r != nil {
|
|
||||||
reqDump = fmt.Sprintf("%s %s. Data: %s", r.Method, r.URL.String(), r.Form.Encode())
|
|
||||||
}
|
|
||||||
if resp != nil {
|
|
||||||
respDump, _ = httputil.DumpResponse(resp, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Log.Write([]byte(fmt.Sprintf("Request: %s\nResponse: %s\n", reqDump, string(respDump))))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user