Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove setcontentlength from reader flow #882

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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