//
// Caller should close r.Body when done reading from it.
func (c *Client) Get(url string) (r *Response, err os.Error) {
+ return c.sendFollowingRedirects("GET", url)
+}
+
+func (c *Client) sendFollowingRedirects(method, url string) (r *Response, err os.Error) {
// TODO: if/when we add cookie support, the redirected request shouldn't
// necessarily supply the same cookies as the original.
var base *URL
for redirect := 0; ; redirect++ {
var req Request
- req.Method = "GET"
+ req.Method = method
req.Header = make(Header)
if base == nil {
req.URL, err = ParseURL(url)
return
}
- err = &URLError{"Get", url, err}
+ err = &URLError{method[0:1] + strings.ToLower(method[1:]), url, err}
return
}
return bytes.NewBuffer([]byte(EncodeQuery(m)))
}
-// Head issues a HEAD to the specified URL.
+// Head issues a HEAD to the specified URL. If the response is one of the
+// following redirect codes, Head follows the redirect after calling the
+// Client's CheckRedirect function.
+//
+// 301 (Moved Permanently)
+// 302 (Found)
+// 303 (See Other)
+// 307 (Temporary Redirect)
//
// Head is a wrapper around DefaultClient.Head
func Head(url string) (r *Response, err os.Error) {
return DefaultClient.Head(url)
}
-// Head issues a HEAD to the specified URL.
+// Head issues a HEAD to the specified URL. If the response is one of the
+// following redirect codes, Head follows the redirect after calling the
+// Client's CheckRedirect function.
+//
+// 301 (Moved Permanently)
+// 302 (Found)
+// 303 (See Other)
+// 307 (Temporary Redirect)
func (c *Client) Head(url string) (r *Response, err os.Error) {
- var req Request
- req.Method = "HEAD"
- if req.URL, err = ParseURL(url); err != nil {
- return
- }
- return send(&req, c.Transport)
+ return c.sendFollowingRedirects("HEAD", url)
}
t.Errorf("with default client, expected error %q, got %q", e, g)
}
+ // HEAD request should also have the ability to follow redirects.
+ _, err = c.Head(ts.URL)
+ if e, g := "Head /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
+ t.Errorf("with default client, expected error %q, got %q", e, g)
+ }
+
var checkErr os.Error
var lastVia []*Request
c = &Client{CheckRedirect: func(_ *Request, via []*Request) os.Error {