I am writing a benchmark test for a redirect script.
I wisg my program to query certain URL that redirects to AppStore. But I do not wish to download AppStore page.
You need to use an http.Transport instead of an http.Client. Transport is lower-level and does not follow redirects.
req, err := http.NewRequest("GET", "http://example.com/redirectToAppStore", nil)
// ...
resp, err := http.DefaultTransport.RoundTrip(req)
For completeness' sake, you can use an http.Client
and not follow redirects. http.Client
has a CheckRedirect field which is a function. It is called before following any redirection.
If this function returns an error, then httpClient.Do(...)
will not follow the redirect (see doFollowingRedirects() function in Go's source code) and instead will return an error (its concrete type will be url.Error
, and its URL
field will be the redirect-to URL, aka the Location header value, see this code).
You can see my gocrawl library for a concrete example of this use.