Skip to content

Commit 1102c70

Browse files
committed
net/http: teach NewRequest that NoBody has ContentLength zero
NoBody is new in Go 1.8. Found while investigating #18117 Change-Id: I6bda030f358e2270f090d108cb3a89c8a2665fcb Reviewed-on: https://go-review.googlesource.com/33714 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 2cfb6d5 commit 1102c70

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/net/http/request.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,9 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
785785
return ioutil.NopCloser(&r), nil
786786
}
787787
default:
788-
req.ContentLength = -1 // unknown
788+
if body != NoBody {
789+
req.ContentLength = -1 // unknown
790+
}
789791
}
790792
// For client requests, Request.ContentLength of 0
791793
// means either actually 0, or unknown. The only way

src/net/http/request_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ func TestNewRequestGetBody(t *testing.T) {
825825
}
826826
}
827827

828+
func TestNewRequestNoBody(t *testing.T) {
829+
req, err := NewRequest("GET", "http://foo.com/", NoBody)
830+
if err != nil {
831+
t.Fatal(err)
832+
}
833+
if req.ContentLength != 0 {
834+
t.Errorf("ContentLength = %d; want 0", req.ContentLength)
835+
}
836+
}
837+
828838
func testMissingFile(t *testing.T, req *Request) {
829839
f, fh, err := req.FormFile("missing")
830840
if f != nil {

0 commit comments

Comments
 (0)