Skip to content

Commit

Permalink
feat!: remove SetContentLength for io.Reader request flow #878
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Oct 6, 2024
1 parent 4a33061 commit 8979813
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
24 changes: 7 additions & 17 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func parseRequestBody(c *Client, r *Request) error {
return err
}
}
} else {
r.Body = nil // if the payload is not supported by HTTP verb, set explicit nil
}

// by default resty won't set content length, you can if you want to :)
Expand All @@ -212,10 +214,8 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.initClientTrace()

if r.bodyBuf == nil {
if reader, ok := r.Body.(io.Reader); ok && isPayloadSupported(r.Method, c.AllowGetMethodPayload()) {
if reader, ok := r.Body.(io.Reader); ok {
r.RawRequest, err = http.NewRequestWithContext(r.Context(), r.Method, r.URL, reader)
} else if r.setContentLength {
r.RawRequest, err = http.NewRequestWithContext(r.Context(), r.Method, r.URL, http.NoBody)
} else {
r.RawRequest, err = http.NewRequestWithContext(r.Context(), r.Method, r.URL, nil)
}
Expand Down Expand Up @@ -552,20 +552,10 @@ func handleRequestBody(c *Client, r *Request) error {
r.bodyBuf = acquireBuffer()

switch body := r.Body.(type) {
case io.Reader:
// TODO create pass through reader to capture content-length, really needed??
if r.setContentLength { // keep backward compatibility
if _, err := r.bodyBuf.ReadFrom(body); err != nil {
releaseBuffer(r.bodyBuf)
return err
}
r.Body = nil
} else {
// Otherwise buffer less processing for `io.Reader`, sounds good.
releaseBuffer(r.bodyBuf)
r.bodyBuf = nil
return nil
}
case io.Reader: // Resty v3 onwards io.Reader used as-is with the request body
releaseBuffer(r.bodyBuf)
r.bodyBuf = nil
return nil
case []byte:
r.bodyBuf.Write(body)
case string:
Expand Down
29 changes: 0 additions & 29 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,28 +597,6 @@ func TestParseRequestBody(t *testing.T) {
},
expectedContentType: jsonContentType,
},
{
name: "io.Reader body with SetContentLength by request",
initRequest: func(r *Request) {
r.SetBody(bytes.NewBufferString("foo")).
SetContentLength(true)
},
expectedBodyBuf: []byte("foo"),
expectedContentLength: "3",
expectedContentType: jsonContentType,
},
{
name: "io.Reader body with SetContentLength by client",
initClient: func(c *Client) {
c.SetContentLength(true)
},
initRequest: func(r *Request) {
r.SetBody(bytes.NewBufferString("foo"))
},
expectedBodyBuf: []byte("foo"),
expectedContentLength: "3",
expectedContentType: jsonContentType,
},
{
name: "form data by request",
initRequest: func(r *Request) {
Expand Down Expand Up @@ -748,13 +726,6 @@ func TestParseRequestBody(t *testing.T) {
expectedContentType: "text/xml",
expectedContentLength: "41",
},
{
name: "body with errorReader",
initRequest: func(r *Request) {
r.SetBody(&errorReader{}).SetContentLength(true)
},
wantErr: true,
},
{
name: "unsupported type",
initRequest: func(r *Request) {
Expand Down

0 comments on commit 8979813

Please sign in to comment.