Skip to content

Fix/missing content length header on empty post req #1003

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

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func parseRequestBody(c *Client, r *Request) error {
}
case len(c.FormData) > 0 || len(r.FormData) > 0: // Handling Form Data
handleFormData(c, r)
case r.Body == nil && r.bodyBuf == nil: // Handling Request body when nil body
// Go http library omits Content-Length if body is nil; use http.NoBody to force it if SetContentLength is true
r.Body = http.NoBody
fallthrough
case r.Body != nil: // Handling Request body
handleContentType(c, r)

Expand Down Expand Up @@ -315,6 +319,7 @@ func createCurlCmd(c *Client, r *Request) (err error) {
}
*r.resultCurlCmd = buildCurlRequest(r.RawRequest, c.httpClient.Jar)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,15 @@ func Test_parseRequestBody(t *testing.T) {
r.SetContentLength(true)
},
expectedContentLength: "0",
expectedBodyBuf: []byte{},
},
{
name: "empty body with SetContentLength by client",
init: func(c *Client, r *Request) {
c.SetContentLength(true)
},
expectedContentLength: "0",
expectedBodyBuf: []byte{},
},
{
name: "string body",
Expand Down
24 changes: 24 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2282,3 +2282,27 @@ func TestRequestGH917(t *testing.T) {
}
wg.Wait()
}

func TestSetContentLengthTrueWithNilBody(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

c := dc()

c.OnBeforeRequest(func(client *Client, request *Request) error {
request.
SetBody(nil).
SetContentLength(true)

return nil
})

c = c.SetBaseURL(ts.URL)

resp, err := c.R().Execute("POST", "/check-request-content-length")

assertNil(t, err)

// when body is nil and SetContentLength is true expect Content-Length == "0"
assertEqual(t, "0", resp.Header().Get("Request-Content-Length"))
}
4 changes: 4 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ func createPostServer(t *testing.T) *httptest.Server {
}
http.SetCookie(w, &cookie)
w.WriteHeader(http.StatusOK)
case "/check-request-content-length":
// test endpoint for checking the server receives the correct Content-Length Header from a request
w.Header().Add("Request-Content-Length", r.Header.Get("Content-Length"))
w.WriteHeader(http.StatusOK)
}
}
})
Expand Down