Selectively follow call forwarding in Go

I'm trying to write a twitter reader that resolves the final URLs of shortened links, etc., but gives me a URL on the way to a list of manually defined templates. The reason for this is because I don’t want the payer URL to end up being specified, but earlier.

As far as I can tell, the way to do this is to write my own client based on the default RoundTripper , because returning an error from the custom CheckRedirect function interrupts the client without giving an answer.

Is there a way to use client by default and write a list of URLs / a specific URL from a custom CheckRedirect function?

+1
source share
1 answer

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) } 
+3
source

Source: https://habr.com/ru/post/1241836/


All Articles