Struct Proxy

pub struct Proxy { /* private fields */ }

Configuration of a proxy that a Client should pass requests to.

A Proxy has a couple pieces to it:

For instance, let's look at Proxy::http:

# fn run() -> Result<(), Box<dyn std::error::Error>> {
let proxy = reqwest::Proxy::http("https://secure.example")?;
# Ok(())
# }

This proxy will intercept all HTTP requests, and make use of the proxy at https://secure.example. A request to http://hyper.rs will talk to your proxy. A request to https://hyper.rs will not.

Multiple Proxy rules can be configured for a Client. The Client will check each Proxy in the order it was added. This could mean that a Proxy added first with eager intercept rules, such as Proxy::all, would prevent a Proxy later in the list from ever working, so take care.

By enabling the "socks" feature it is possible to use a socks proxy:

# fn run() -> Result<(), Box<dyn std::error::Error>> {
let proxy = reqwest::Proxy::http("socks5://192.168.1.1:9000")?;
# Ok(())
# }

Implementations

impl Proxy

fn http<U: IntoProxy>(proxy_scheme: U) -> Result<Proxy>

Proxy all HTTP traffic to the passed URL.

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::builder()
    .proxy(reqwest::Proxy::http("https://my.prox")?)
    .build()?;
# Ok(())
# }
# fn main() {}
fn https<U: IntoProxy>(proxy_scheme: U) -> Result<Proxy>

Proxy all HTTPS traffic to the passed URL.

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::builder()
    .proxy(reqwest::Proxy::https("https://example.prox:4545")?)
    .build()?;
# Ok(())
# }
# fn main() {}
fn all<U: IntoProxy>(proxy_scheme: U) -> Result<Proxy>

Proxy all traffic to the passed URL.

"All" refers to https and http URLs. Other schemes are not recognized by reqwest.

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::builder()
    .proxy(reqwest::Proxy::all("http://pro.xy")?)
    .build()?;
# Ok(())
# }
# fn main() {}
fn custom<F, U: IntoProxy>(fun: F) -> Proxy
where
    F: Fn(&Url) -> Option<U> + Send + Sync + 'static,

Provide a custom function to determine what traffic to proxy to where.

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let target = reqwest::Url::parse("https://my.prox")?;
let client = reqwest::Client::builder()
    .proxy(reqwest::Proxy::custom(move |url| {
        if url.host_str() == Some("hyper.rs") {
            Some(target.clone())
        } else {
            None
        }
    }))
    .build()?;
# Ok(())
# }
# fn main() {}
fn basic_auth(self, username: &str, password: &str) -> Proxy

Set the Proxy-Authorization header using Basic auth.

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let proxy = reqwest::Proxy::https("http://localhost:1234")?
    .basic_auth("Aladdin", "open sesame");
# Ok(())
# }
# fn main() {}
fn custom_http_auth(self, header_value: HeaderValue) -> Proxy

Set the Proxy-Authorization header to a specified value.

Example

# extern crate reqwest;
# use reqwest::header::*;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let proxy = reqwest::Proxy::https("http://localhost:1234")?
    .custom_http_auth(HeaderValue::from_static("justletmeinalreadyplease"));
# Ok(())
# }
# fn main() {}
fn headers(self, headers: HeaderMap) -> Proxy

Adds a Custom Headers to Proxy Adds custom headers to this Proxy

Example

# extern crate reqwest;
# use reqwest::header::*;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, "reqwest".parse().unwrap());
let proxy = reqwest::Proxy::https("http://localhost:1234")?
    .headers(headers);
# Ok(())
# }
# fn main() {}
fn no_proxy(self, no_proxy: Option<NoProxy>) -> Proxy

Adds a No Proxy exclusion list to this Proxy

Example

# extern crate reqwest;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let proxy = reqwest::Proxy::https("http://localhost:1234")?
    .no_proxy(reqwest::NoProxy::from_string("direct.tld, sub.direct2.tld"));
# Ok(())
# }
# fn main() {}

Trait Implementations

impl Clone for Proxy

fn clone(&self) -> Self

impl Debug for Proxy

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl !Freeze for Proxy

impl !RefUnwindSafe for Proxy

impl !UnwindSafe for Proxy

impl Send for Proxy

impl Sync for Proxy

impl Unpin for Proxy

impl UnsafeUnpin for Proxy

Blanket Implementations

impl<T> Any for Proxy where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Proxy where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Proxy where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Proxy where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Proxy

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Proxy

impl<T> PolicyExt for Proxy where T: ?Sized,

fn and<P, B, E>(self, other: P) -> And<T, P>
where
    T: Sized + Policy<B, E>,
    P: Policy<B, E>,
fn or<P, B, E>(self, other: P) -> Or<T, P>
where
    T: Sized + Policy<B, E>,
    P: Policy<B, E>,

impl<T> ToOwned for Proxy where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for Proxy

impl<T, U> Into<U> for Proxy where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Proxy where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Proxy where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>