Skip to content

Commit

Permalink
Merge pull request #160 from monzo/warn-BodyBytes
Browse files Browse the repository at this point in the history
Warn that BodyBytes functions should be called on reference
  • Loading branch information
WillSewell authored Jan 19, 2023
2 parents ac30164 + f02e8cd commit 578c87d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func (r *Request) Write(b []byte) (n int, err error) {
//
// If consume is true, this is equivalent to ioutil.ReadAll; if false, the caller will observe the body to be in
// the same state that it was before (ie. any remaining unread body can be read again).
//
// Warning: if consume is false, you must ensure this is called on a pointer receiver (*Request) and not a
// Request value. This is because the Response.Body referenced by the caller needs to be mutated.
func (r *Request) BodyBytes(consume bool) ([]byte, error) {
if consume {
defer r.Body.Close()
Expand Down
3 changes: 3 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ func (r *Response) Write(b []byte) (n int, err error) {

// BodyBytes fully reads the response body and returns the bytes read. If consume is false, the body is copied into a
// new buffer such that it may be read again.
//
// Warning: if consume is false, you must ensure this is called on a pointer receiver (*Response) and not a
// Response value. This is because the Response.Body referenced by the caller needs to be mutated.
func (r *Response) BodyBytes(consume bool) ([]byte, error) {
if consume {
defer r.Body.Close()
Expand Down

0 comments on commit 578c87d

Please sign in to comment.