Struct Policy
pub struct Policy { /* private fields */ }
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.
limitedcan be used have the same as the default behavior, but adjust the allowed maximum redirect hops in a chain.nonecan be used to disable all redirect behavior.customcan be used to create a customized policy.
Implementations
impl Policy
fn limited(max: usize) -> SelfCreate a
Policywith a maximum number of redirects.An
Errorwill be returned if the max is reached.fn none() -> SelfCreate a
Policythat does not follow any redirect.fn custom<T>(policy: T) -> Self where T: Fn(Attempt<'_>) -> Action + Send + Sync + 'static,Create a custom
Policyusing the passed function.Note
The default
Policyhandles 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
Attemptargument passed to the closure.Actions can be conveniently created from methods on the
Attempt.Example
# use reqwest::{Error, redirect}; # # fn run() -> Result<(), Error> { let custom = redirect::Policy::custom(|attempt| { if attempt.previous().len() > 5 { attempt.error("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()?; # Ok(()) # }fn redirect(&self, attempt: Attempt<'_>) -> ActionApply this policy to a given
Attemptto produce aAction.Note
This method can be used together with
Policy::custom()to construct onePolicythat wraps another.Example
# use reqwest::{Error, redirect}; # # fn run() -> Result<(), Error> { let custom = redirect::Policy::custom(|attempt| { eprintln!("{}, Location: {:?}", attempt.status(), attempt.url()); redirect::Policy::default().redirect(attempt) }); # Ok(()) # }
Trait Implementations
impl Debug for Policy
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Policy
fn default() -> Policy