Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DefaultRedirectPolicy #403

Merged
merged 1 commit into from
Dec 2, 2024
Merged

Conversation

rosahaj
Copy link
Contributor

@rosahaj rosahaj commented Dec 1, 2024

There's currently no easy and intuitive way to change a client's redirect policy back to the default redirect policy.

For example: Let's say someone wants to send a GET request to a given URL, but is only interested in the Location header and the cookies of the response. They can use c.SetRedirectPolicy(req.NoRedirectPolicy()) to disable redirects and manually grab the Location header, but there's no simple way to "reset" the redirect policy back to the default one.

They could go through req's source code until they find the following function, only to realize that they can achieve the same thing by setting the redirect policy to req.MaxRedirectPolicy(10):

req/client.go

Lines 324 to 332 in 24b0c84

func (c *Client) defaultCheckRedirect(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
if c.DebugLog {
c.log.Debugf("<redirect> %s %s", req.Method, req.URL.String())
}
return nil
}

This PR introduces req.DefaultRedirectPolicy(), which is essentially just an alias for req.MaxRedirectPolicy(10), but greatly improves the developer experience in scenarios where someone wants to set a specific redirect policy, send out a couple of requests and then revert back to the default one.

For relevant documentation changes, see imroc/req-website#239 (comment)

@imroc imroc merged commit 55733ac into imroc:master Dec 2, 2024
2 checks passed
@imroc
Copy link
Owner

imroc commented Dec 2, 2024

Thanks

@rosahaj rosahaj deleted the add-default-redirect-policy branch December 5, 2024 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants