[][src]Struct hreq::Agent

pub struct Agent { /* fields omitted */ }

Agents provide redirects, connection pooling, cookies and retries.

Every request is sent through an agent, also when using the extension trait (Request.send()). When using the trait, the agent is intantiated with default parameters and lives for the length of the request.

To amend the default parameters, or reuse an agent over many requests, use Agent::new() and send the request using agent.send(req).

The default agent have the following settings:

  • Redirects: 5
  • Retries: 5
  • Connection pooling: on
  • Cookies: on

The settings can be changed, and are used for the next .send() call. It is possible to change the settings between calls.

use hreq::prelude::*;
use hreq::Agent;

let mut agent = Agent::new();
agent.retries(0); // disable all retries

let req = Request::get("https://www.google.com")
    .with_body(()).unwrap();

let res = agent.send(req).block();

Implementations

impl Agent[src]

pub fn new() -> Self[src]

Creates a new agent with default parameters.

use hreq::Agent;

let agent = Agent::new();

pub fn redirects(&mut self, amount: u8)[src]

Changes number of redirects.

Defaults to 5. Set to 0 to disable redirects.

The number of redirects will be used for the next call to .send().

use hreq::Agent;

let mut agent = Agent::new();
agent.redirects(0);

pub fn retries(&mut self, amount: u8)[src]

Changes the number of retry attempts.

Defaults to 5. Set to 0 to disable retries.

The number of retries will be used for the next call to .send().

use hreq::Agent;

let mut agent = Agent::new();
agent.retries(0);

pub fn pooling(&mut self, enabled: bool)[src]

Turns connection pooling on or off.

Defaults to true. Set to false to disable connection pooling.

The setting will be used for the next call to .send().

When set to false any existing connection currently pooled will be dropped.

use hreq::Agent;

let mut agent = Agent::new();
agent.pooling(false);

pub fn cookies(&mut self, enabled: bool)[src]

Turns on or off the use of cookies.

Defaults to true. Set to false to disable use of cookies.

The setting will be used for the next call to .send().

When set to false, any previous collected cookie will be dropped.

use hreq::Agent;

let mut agent = Agent::new();
agent.cookies(false);

pub fn get_cookies(&self, uri: &Uri) -> Vec<&Cookie<'static>>[src]

Get all cookies held in this agent matching the given uri.

pub async fn send<B: Into<Body>, '_>(
    &'_ mut self,
    req: Request<B>
) -> Result<Response<Body>, Error>
[src]

Sends a request using this agent.

The parameters configured in the agent are used for the request.

Depending on agent settings, connections are pooled and cookies reused between repeated calls to send().

use hreq::prelude::*;
use hreq::Agent;

let mut agent = Agent::new();
agent.retries(0);
agent.redirects(0);

let req = Request::get("https://fails-badly-yeah")
    .with_body(()).unwrap();

let res = agent.send(req).block();

assert!(res.is_err());
assert!(res.unwrap_err().is_io());

Trait Implementations

impl Debug for Agent[src]

impl Default for Agent[src]

Auto Trait Implementations

impl !RefUnwindSafe for Agent

impl Send for Agent

impl Sync for Agent

impl Unpin for Agent

impl !UnwindSafe for Agent

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Sealed<T> for T where
    T: ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> WithSubscriber for T[src]