diff --git a/client.go b/client.go index f40d241..037d0d5 100644 --- a/client.go +++ b/client.go @@ -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: diff --git a/client_test.go b/client_test.go index b3f8e6d..5af8c5f 100644 --- a/client_test.go +++ b/client_test.go @@ -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