The client request will still return the last valid Response in cases where your custom CheckResponse gives an error (as indicated in the comments).
http://golang.org/pkg/net/http/#Client
If CheckRedirect returns an error, the Get Client method returns both the previous Response error and CheckRedirect (wrapped in url.Error) instead of issuing a request.
If you maintain a list of "known" paywall-urls, you can abort the paywall redirection in your CheckResponse using the custom error type ( Paywalled in the example below). Later, your error handling code should consider this type of error as a special (non-error) case.
Example:
package main import ( "errors" "fmt" "net/http" "net/url" ) var Paywalled = errors.New("next redirect would hit a paywall") var badHosts = map[string]error{ "registration.ft.com": Paywalled, } var client = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { // NB: when used in production, also check for redirect loops return badHosts[req.URL.Host] }, } func main() { resp, err := client.Get("http://on.ft.com/14pQBYE") // ignore non-nil err if it a `Paywalled` wrapped in url.Error if e, ok := err.(*url.Error); (ok && e.Err != Paywalled) || (!ok && err != nil) { fmt.Println("error: ", err) return } resp.Body.Close() fmt.Println(resp.Request.URL) }
source share