Struct reqwest::RedirectPolicy
source · [−]pub struct RedirectPolicy { /* private fields */ }
Expand description
A type that controls the policy on how to handle the following of redirects.
The default value will catch redirect loops, and has a maximum of 10 redirects it will follow in a chain before returning an error.
limited
can be used have the same as the default behavior, but adjust the allowed maximum redirect hops in a chain.none
can be used to disable all redirect behavior.custom
can be used to create a customized policy.
Implementations
sourceimpl RedirectPolicy
impl RedirectPolicy
sourcepub fn limited(max: usize) -> RedirectPolicy
pub fn limited(max: usize) -> RedirectPolicy
Create a RedirectPolicy with a maximum number of redirects.
An Error
will be returned if the max is reached.
sourcepub fn none() -> RedirectPolicy
pub fn none() -> RedirectPolicy
Create a RedirectPolicy that does not follow any redirect.
sourcepub fn custom<T>(policy: T) -> RedirectPolicy where
T: Fn(RedirectAttempt<'_>) -> RedirectAction + Send + Sync + 'static,
pub fn custom<T>(policy: T) -> RedirectPolicy where
T: Fn(RedirectAttempt<'_>) -> RedirectAction + Send + Sync + 'static,
Create a custom RedirectPolicy using the passed function.
Note
The default RedirectPolicy handles redirect loops and a maximum loop chain, but the custom variant does not do that for you automatically. The custom policy should have some way of handling those.
Information on the next request and previous requests can be found
on the RedirectAttempt
argument passed to the closure.
Actions can be conveniently created from methods on the
RedirectAttempt
.
Example
let custom = RedirectPolicy::custom(|attempt| {
if attempt.previous().len() > 5 {
attempt.too_many_redirects()
} else if attempt.url().host_str() == Some("example.domain") {
// prevent redirects to 'example.domain'
attempt.stop()
} else {
attempt.follow()
}
});
let client = reqwest::Client::builder()
.redirect(custom)
.build()?;
sourcepub fn redirect(&self, attempt: RedirectAttempt<'_>) -> RedirectAction
pub fn redirect(&self, attempt: RedirectAttempt<'_>) -> RedirectAction
Apply this policy to a given RedirectAttempt
to produce a RedirectAction
.
Note
This method can be used together with RedirectPolicy::custom() to construct one RedirectPolicy that wraps another.
Example
let custom = RedirectPolicy::custom(|attempt| {
eprintln!("{}, Location: {:?}", attempt.status(), attempt.url());
RedirectPolicy::default().redirect(attempt)
});
Trait Implementations
sourceimpl Debug for RedirectPolicy
impl Debug for RedirectPolicy
sourceimpl Default for RedirectPolicy
impl Default for RedirectPolicy
sourcefn default() -> RedirectPolicy
fn default() -> RedirectPolicy
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl !RefUnwindSafe for RedirectPolicy
impl Send for RedirectPolicy
impl Sync for RedirectPolicy
impl Unpin for RedirectPolicy
impl !UnwindSafe for RedirectPolicy
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more