Struct reqwest::ClientBuilder
source · [−]pub struct ClientBuilder { /* private fields */ }
Expand description
A ClientBuilder
can be used to create a Client
with custom configuration.
Example
use std::time::Duration;
let client = reqwest::Client::builder()
.gzip(true)
.timeout(Duration::from_secs(10))
.build()?;
Implementations
sourceimpl ClientBuilder
impl ClientBuilder
sourcepub fn new() -> ClientBuilder
pub fn new() -> ClientBuilder
Constructs a new ClientBuilder
.
This is the same as Client::builder()
.
sourcepub fn build(self) -> Result<Client>
pub fn build(self) -> Result<Client>
Returns a Client
that uses this ClientBuilder
configuration.
Errors
This method fails if TLS backend cannot be initialized, or the resolver cannot load the system configuration.
sourcepub fn no_proxy(self) -> ClientBuilder
pub fn no_proxy(self) -> ClientBuilder
Disable proxy setting.
sourcepub fn use_sys_proxy(self) -> ClientBuilder
pub fn use_sys_proxy(self) -> ClientBuilder
Enable system proxy setting.
sourcepub fn tcp_nodelay(self) -> ClientBuilder
pub fn tcp_nodelay(self) -> ClientBuilder
Set that all sockets have SO_NODELAY
set to true
.
sourcepub fn use_default_tls(self) -> ClientBuilder
pub fn use_default_tls(self) -> ClientBuilder
Use native TLS backend.
sourcepub fn add_root_certificate(self, cert: Certificate) -> ClientBuilder
pub fn add_root_certificate(self, cert: Certificate) -> ClientBuilder
Add a custom root certificate.
This allows connecting to a server that has a self-signed certificate for example. This does not replace the existing trusted store.
Example
// read a local binary DER encoded certificate
let mut buf = Vec::new();
File::open("my-cert.der")?.read_to_end(&mut buf)?;
// create a certificate
let cert = reqwest::Certificate::from_der(&buf)?;
// get a client builder
let client = reqwest::Client::builder()
.add_root_certificate(cert)
.build()?;
Errors
This method fails if adding root certificate was unsuccessful.
sourcepub fn identity(self, identity: Identity) -> ClientBuilder
pub fn identity(self, identity: Identity) -> ClientBuilder
Sets the identity to be used for client certificate authentication.
Example
// read a local PKCS12 bundle
let mut buf = Vec::new();
#[cfg(feature = "default-tls")]
File::open("my-ident.pfx")?.read_to_end(&mut buf)?;
#[cfg(feature = "rustls-tls")]
File::open("my-ident.pem")?.read_to_end(&mut buf)?;
#[cfg(feature = "default-tls")]
// create an Identity from the PKCS#12 archive
let pkcs12 = reqwest::Identity::from_pkcs12_der(&buf, "my-privkey-password")?;
#[cfg(feature = "rustls-tls")]
// create an Identity from the PEM file
let pkcs12 = reqwest::Identity::from_pem(&buf)?;
// get a client builder
let client = reqwest::Client::builder()
.identity(pkcs12)
.build()?;
sourcepub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostname: bool
) -> ClientBuilder
pub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostname: bool
) -> ClientBuilder
Controls the use of hostname verification.
Defaults to false
.
Warning
You should think very carefully before you use this method. If hostname verification is not used, any valid certificate for any site will be trusted for use from any other. This introduces a significant vulnerability to man-in-the-middle attacks.
sourcepub fn danger_accept_invalid_certs(
self,
accept_invalid_certs: bool
) -> ClientBuilder
pub fn danger_accept_invalid_certs(
self,
accept_invalid_certs: bool
) -> ClientBuilder
Controls the use of certificate validation.
Defaults to false
.
Warning
You should think very carefully before using this method. If invalid certificates are trusted, any certificate for any site will be trusted for use. This includes expired certificates. This introduces significant vulnerabilities, and should only be used as a last resort.
sourcepub fn default_headers(self, headers: HeaderMap) -> ClientBuilder
pub fn default_headers(self, headers: HeaderMap) -> ClientBuilder
Sets the default headers for every request.
Example
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret"));
// get a client builder
let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
let res = client.get("https://www.rust-lang.org").send()?;
Override the default headers:
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret"));
// get a client builder
let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
let res = client
.get("https://www.rust-lang.org")
.header(header::AUTHORIZATION, "token")
.send()?;
sourcepub fn gzip(self, enable: bool) -> ClientBuilder
pub fn gzip(self, enable: bool) -> ClientBuilder
Enable auto gzip decompression by checking the ContentEncoding response header.
If auto gzip decompresson is turned on:
- When sending a request and if the request’s headers do not already contain
an
Accept-Encoding
andRange
values, theAccept-Encoding
header is set togzip
. The body is not automatically inflated. - When receiving a response, if it’s headers contain a
Content-Encoding
value that equals togzip
, both valuesContent-Encoding
andContent-Length
are removed from the headers’ set. The body is automatically deinflated.
Default is enabled.
sourcepub fn proxy(self, proxy: Proxy) -> ClientBuilder
pub fn proxy(self, proxy: Proxy) -> ClientBuilder
Add a Proxy
to the list of proxies the Client
will use.
sourcepub fn redirect(self, policy: RedirectPolicy) -> ClientBuilder
pub fn redirect(self, policy: RedirectPolicy) -> ClientBuilder
Set a RedirectPolicy
for this client.
Default will follow redirects up to a maximum of 10.
sourcepub fn referer(self, enable: bool) -> ClientBuilder
pub fn referer(self, enable: bool) -> ClientBuilder
Enable or disable automatic setting of the Referer
header.
Default is true
.
sourcepub fn timeout<T>(self, timeout: T) -> ClientBuilder where
T: Into<Option<Duration>>,
pub fn timeout<T>(self, timeout: T) -> ClientBuilder where
T: Into<Option<Duration>>,
Set a timeout for connect, read and write operations of a Client
.
Default is 30 seconds.
Pass None
to disable timeout.
sourcepub fn max_idle_per_host(self, max: usize) -> ClientBuilder
pub fn max_idle_per_host(self, max: usize) -> ClientBuilder
Sets the maximum idle connection per host allowed in the pool.
Default is usize::MAX (no limit).
sourcepub fn connect_timeout<T>(self, timeout: T) -> ClientBuilder where
T: Into<Option<Duration>>,
pub fn connect_timeout<T>(self, timeout: T) -> ClientBuilder where
T: Into<Option<Duration>>,
Set a timeout for only the connect phase of a Client
.
Default is None
.
sourcepub fn h2_prior_knowledge(self) -> ClientBuilder
pub fn h2_prior_knowledge(self) -> ClientBuilder
Only use HTTP/2.
Example
let client = reqwest::Client::builder()
.h2_prior_knowledge()
.build().unwrap();
sourcepub fn http1_title_case_headers(self) -> ClientBuilder
pub fn http1_title_case_headers(self) -> ClientBuilder
Enable case sensitive headers.
Example
let client = reqwest::Client::builder()
.http1_title_case_headers()
.build().unwrap();
sourcepub fn local_address<T>(self, addr: T) -> ClientBuilder where
T: Into<Option<IpAddr>>,
pub fn local_address<T>(self, addr: T) -> ClientBuilder where
T: Into<Option<IpAddr>>,
Bind to a local IP Address
Example
use std::net::IpAddr;
let local_addr = IpAddr::from([12, 4, 1, 8]);
let client = reqwest::Client::builder()
.local_address(local_addr)
.build().unwrap();
Enable a persistent cookie store for the client.
Cookies received in responses will be preserved and included in additional requests.
By default, no cookie store is used.
Example
let client = reqwest::Client::builder()
.cookie_store(true)
.build()
.unwrap();
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for ClientBuilder
impl Send for ClientBuilder
impl Sync for ClientBuilder
impl Unpin for ClientBuilder
impl !UnwindSafe for ClientBuilder
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