diff --git a/client.go b/client.go index 79dc931..4332060 100644 --- a/client.go +++ b/client.go @@ -34,6 +34,7 @@ import ( "net/http" "net/url" "os" + "reflect" "regexp" "strconv" "strings" @@ -154,6 +155,10 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro var bodyReader ReaderFunc var contentLength int64 + if isNil(rawBody) { + return bodyReader, contentLength, nil + } + switch body := rawBody.(type) { // If they gave us a function already, great! Use it. case ReaderFunc: @@ -772,3 +777,7 @@ func (c *Client) StandardClient() *http.Client { Transport: &RoundTripper{Client: c}, } } + +func isNil(v interface{}) bool { + return v == nil || ((reflect.ValueOf(v).Kind() == reflect.Ptr || reflect.ValueOf(v).Kind() == reflect.Slice) && reflect.ValueOf(v).IsNil()) +} diff --git a/client_test.go b/client_test.go index b8a3d9d..df63726 100644 --- a/client_test.go +++ b/client_test.go @@ -842,3 +842,21 @@ func TestClient_StandardClient(t *testing.T) { t.Fatalf("expected %v, got %v", client, v) } } + +func Test_NewRequest(t *testing.T) { + req, err := NewRequest("GET", "localhost:1234", nil) + if err != nil { + t.Fatalf("failed to create a request: %s", err) + } + + var b []byte + b = nil + r, err := NewRequest("GET", "localhost:1234", b) + if err != nil { + t.Fatalf("failed to create a request: %s", err) + } + + if req.body != nil || r.body != nil { + t.Fatalf("expected nil body for both requests, got:%v %v", req.body, r.body) + } +}