Skip to content

Commit a778ac5

Browse files
committed
net/http: make Client follow redirects even if Request.Method is empty
Fixes #12705 Change-Id: I69639d2b03777835b2697ff349a00ccab410aa49 Reviewed-on: https://go-review.googlesource.com/17318 Reviewed-by: Burcu Dogan <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 723605e commit a778ac5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/net/http/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (c *Client) send(req *Request) (*Response, error) {
172172
//
173173
// Generally Get, Post, or PostForm will be used instead of Do.
174174
func (c *Client) Do(req *Request) (resp *Response, err error) {
175-
if req.Method == "GET" || req.Method == "HEAD" {
175+
if req.Method == "" || req.Method == "GET" || req.Method == "HEAD" {
176176
return c.doFollowingRedirects(req, shouldRedirectGet)
177177
}
178178
if req.Method == "POST" || req.Method == "PUT" {
@@ -423,6 +423,9 @@ func (c *Client) doFollowingRedirects(ireq *Request, shouldRedirect func(int) bo
423423
}
424424

425425
method := ireq.Method
426+
if method == "" {
427+
method = "GET"
428+
}
426429
urlErr := &url.Error{
427430
Op: method[0:1] + strings.ToLower(method[1:]),
428431
URL: urlStr,

src/net/http/client_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ func TestClientRedirects(t *testing.T) {
230230
t.Errorf("with default client Do, expected error %q, got %q", e, g)
231231
}
232232

233+
// Requests with an empty Method should also redirect (Issue 12705)
234+
greq.Method = ""
235+
_, err = c.Do(greq)
236+
if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
237+
t.Errorf("with default client Do and empty Method, expected error %q, got %q", e, g)
238+
}
239+
233240
var checkErr error
234241
var lastVia []*Request
235242
c = &Client{CheckRedirect: func(_ *Request, via []*Request) error {

0 commit comments

Comments
 (0)