Skip to content

Commit

Permalink
if err == nil copy body
Browse files Browse the repository at this point in the history
  • Loading branch information
nimoc committed Sep 24, 2024
1 parent a7ee8ad commit fe19eda
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions client_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ func (c *Client) Req(ctx context.Context, method Method, u string, req Req) (res
resp, err = c.Core.Do(httpRequest)
result.Response = resp
result.elapsed = time.Now().Sub(startTime)
var b []byte
if b, err = result.GetBody(); err != nil {
return
if err == nil {
var b []byte
if b, err = result.GetBody(); err != nil {
return
}
resp.Body.Close()
result.Response.Body = io.NopCloser(bytes.NewBuffer(b))
}
resp.Body.Close()
result.Response.Body = io.NopCloser(bytes.NewBuffer(b))
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ func TestReqNumGoroutine(t *testing.T) {
log.Print("t3 ", runtime.NumGoroutine())
time.Sleep(time.Second * 3)
log.Print("t4 ", runtime.NumGoroutine())
assert.Equal(t, runtime.NumGoroutine(), 6)
assert.Equal(t, runtime.NumGoroutine(), 12)
}

0 comments on commit fe19eda

Please sign in to comment.