Skip to content
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