Skip to content

Instantly share code, notes, and snippets.

@VojtechVitek
Last active July 7, 2023 19:10
Show Gist options
  • Save VojtechVitek/eb0171fc65f945a8641e to your computer and use it in GitHub Desktop.
Save VojtechVitek/eb0171fc65f945a8641e to your computer and use it in GitHub Desktop.
Golang: Workaround for too many redirects - "stopped after 10 redirects" error
sourceURL := "http://example.com"
// Resolve URL up to 12 redirects.
client := &http.Client{
CheckRedirect: func() func(req *http.Request, via []*http.Request) error {
redirects := 0
return func(req *http.Request, via []*http.Request) error {
if redirects > 12 {
return errors.New("stopped after 12 redirects")
}
redirects++
return nil
}
}(),
}
req, err := http.NewRequest("GET", sourceURL, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
finalURL = resp.Request.URL.String()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment