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

Properly parsing request body when http.NoBody is used #168

Open
wants to merge 1 commit into
base: main
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
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro
var bodyReader ReaderFunc
var contentLength int64

// Do not return a body reader func in the event no body should be sent.
// Without this check, a literal "0" will be sent as the request body.
if rawBody == http.NoBody {
return nil, 0, nil
}

switch body := rawBody.(type) {
// If they gave us a function already, great! Use it.
case ReaderFunc:
Expand Down
12 changes: 12 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ func TestFromRequest(t *testing.T) {
if req.ContentLength != 2 {
t.Fatalf("bad ContentLength: %d", req.ContentLength)
}

// Works with http.NoBody.
httpReq, err = http.NewRequest("GET", "/", http.NoBody)
if err != nil {
t.Fatalf("err: %v", err)
}
if req, err = FromRequest(httpReq); err != nil {
t.Fatalf("err: %v", err)
}
if req.body != nil {
t.Fatal("expected no body reader func")
}
}

// Since normal ways we would generate a Reader have special cases, use a
Expand Down