Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
package main
import (
"fmt"
"net/http"
)
func main() {
client := &http.Client{
Transport: &http.Transport{},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) > 0 {
fmt.Printf("%s -> %s\n", via[len(via)-1].URL.String(), req.URL.String())
}
return nil
},
}
res, err := client.Get("...")
}
By “get a list of all redirects” I meant get a variable with a list that can then be used further.
Something like here (Java Apache HttpClient)
public List getAllRedirectLocations(String link) throws ClientProtocolException, IOException {
List redirectLocations = null;
CloseableHttpResponse response = null;
try {
HttpClientContext context = HttpClientContext.create();
HttpGet httpGet = new HttpGet(link);
response = httpClient.execute(httpGet, context);
// get all redirection locations
redirectLocations = context.getRedirectLocations();
} finally {
if(response != null) {
response.close();
}
}
return redirectLocations;
}
type LogRedirects struct {
Transport http.RoundTripper
}
func (l LogRedirects) RoundTrip(req *http.Request) (resp *http.Response, err error) {
t := l.Transport
if t == nil {
t = http.DefaultTransport
}
resp, err = t.RoundTrip(req)
if err != nil {
return
}
switch resp.StatusCode {
case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect:
log.Println("Request for", req.URL, "redirected with status", resp.StatusCode)
}
return
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question