diff --git a/middleware.go b/middleware.go index c03bcff1..73aeffb1 100644 --- a/middleware.go +++ b/middleware.go @@ -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 :) @@ -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) } @@ -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: diff --git a/middleware_test.go b/middleware_test.go index 0ff2634d..7c30cbd4 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -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) { @@ -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) {