diff --git a/src/net/http/request.go b/src/net/http/request.go index c85713c42c6..9f740422ede 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -581,6 +581,12 @@ func validMethod(method string) bool { // type's documentation for the difference between inbound and outbound // request fields. func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { + if method == "" { + // We document that "" means "GET" for Request.Method, and people have + // relied on that from NewRequest, so keep that working. + // We still enforce validMethod for non-empty methods. + method = "GET" + } if !validMethod(method) { return nil, fmt.Errorf("net/http: invalid method %q", method) } diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go index a95a1d08c92..ddbf8418e1a 100644 --- a/src/net/http/request_test.go +++ b/src/net/http/request_test.go @@ -370,6 +370,13 @@ func TestRequestInvalidMethod(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "invalid method") { t.Errorf("Transport error = %v; want invalid method", err) } + + req, err = NewRequest("", "http://foo.com/", nil) + if err != nil { + t.Errorf("NewRequest(empty method) = %v; want nil", err) + } else if req.Method != "GET" { + t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method) + } } func TestNewRequestContentLength(t *testing.T) {