Last active
July 7, 2023 19:10
-
-
Save VojtechVitek/eb0171fc65f945a8641e to your computer and use it in GitHub Desktop.
Golang: Workaround for too many redirects - "stopped after 10 redirects" error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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