Struct reqwest::blocking::ClientBuilder [−][src]
pub struct ClientBuilder { /* fields omitted */ }
Expand description
A ClientBuilder
can be used to create a Client
with custom configuration.
Example
use std::time::Duration;
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.build()?;
Implementations
Constructs a new ClientBuilder
.
This is the same as Client::builder()
.
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.
Panics
This method panics if called from within an async runtime. See docs on
reqwest::blocking
for details.
pub fn user_agent<V>(self, value: V) -> ClientBuilder where
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn user_agent<V>(self, value: V) -> ClientBuilder where
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Sets the User-Agent
header to be used by this client.
Example
// Name your user agent after your app?
static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);
let client = reqwest::blocking::Client::builder()
.user_agent(APP_USER_AGENT)
.build()?;
let res = client.get("https://www.rust-lang.org").send()?;
Sets the default headers for every request.
Example
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert("X-MY-HEADER", header::HeaderValue::from_static("value"));
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret"));
// Consider marking security-sensitive headers with `set_sensitive`.
let mut auth_value = header::HeaderValue::from_static("secret");
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);
// get a client builder
let client = reqwest::blocking::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("X-MY-HEADER", header::HeaderValue::from_static("value"));
// get a client builder
let client = reqwest::blocking::Client::builder()
.default_headers(headers)
.build()?;
let res = client
.get("https://www.rust-lang.org")
.header("X-MY-HEADER", "new_value")
.send()?;
Disable auto response body gzip decompression.
This method exists even if the optional gzip
feature is not enabled.
This can be used to ensure a Client
doesn’t use gzip decompression
even if another dependency were to enable the optional gzip
feature.
Disable auto response body brotli decompression.
This method exists even if the optional brotli
feature is not enabled.
This can be used to ensure a Client
doesn’t use brotli decompression
even if another dependency were to enable the optional brotli
feature.
Disable auto response body deflate decompression.
This method exists even if the optional deflate
feature is not enabled.
This can be used to ensure a Client
doesn’t use deflate decompression
even if another dependency were to enable the optional deflate
feature.
Set a redirect::Policy
for this client.
Default will follow redirects up to a maximum of 10.
Enable or disable automatic setting of the Referer
header.
Default is true
.
Add a Proxy
to the list of proxies the Client
will use.
Note
Adding a proxy will disable the automatic usage of the “system” proxy.
Clear all Proxies
, so Client
will use no proxy anymore.
This also disables the automatic usage of the “system” proxy.
Set a timeout for connect, read and write operations of a Client
.
Default is 30 seconds.
Pass None
to disable timeout.
Set a timeout for only the connect phase of a Client
.
Default is None
.
Set whether connections should emit verbose logs.
Enabling this option will emit log messages at the TRACE
level
for read and write operations on connections.
Set an optional timeout for idle sockets being kept-alive.
Pass None
to disable timeout.
Default is 90 seconds.
Sets the maximum idle connection per host allowed in the pool.
Send headers as title case instead of lowercase.
Only use HTTP/1.
Allow HTTP/0.9 responses
Only use HTTP/2.
Sets the SETTINGS_INITIAL_WINDOW_SIZE
option for HTTP2 stream-level flow control.
Default is currently 65,535 but may change internally to optimize for common uses.
pub fn http2_initial_connection_window_size(
self,
sz: impl Into<Option<u32>>
) -> ClientBuilder
pub fn http2_initial_connection_window_size(
self,
sz: impl Into<Option<u32>>
) -> ClientBuilder
Sets the max connection-level flow control for HTTP2
Default is currently 65,535 but may change internally to optimize for common uses.
Sets whether to use an adaptive flow control.
Enabling this will override the limits set in http2_initial_stream_window_size
and
http2_initial_connection_window_size
.
Sets the maximum frame size to use for HTTP2.
Default is currently 16,384 but may change internally to optimize for common uses.
Set whether sockets have SO_NODELAY
enabled.
Default is true
.
Bind to a local IP Address.
Example
use std::net::IpAddr;
let local_addr = IpAddr::from([12, 4, 1, 8]);
let client = reqwest::blocking::Client::builder()
.local_address(local_addr)
.build().unwrap();
Set that all sockets have SO_KEEPALIVE
set with the supplied duration.
If None
, the option will not be set.
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 der = std::fs::read("my-cert.der")?;
// create a certificate
let cert = reqwest::Certificate::from_der(&der)?;
// get a client builder
let client = reqwest::blocking::Client::builder()
.add_root_certificate(cert)
.build()?;
Optional
This requires the optional default-tls
, native-tls
, or rustls-tls(-...)
feature to be enabled.
Controls the use of built-in system certificates during certificate validation.
Defaults to true
– built-in system certs will be used.
Optional
This requires the optional default-tls
, native-tls
, or rustls-tls(-...)
feature to be enabled.
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.
Set the minimum required TLS version for connections.
By default the TLS backend’s own default is used.
Errors
A value of tls::Version::TLS_1_3
will cause an error with the
native-tls
/default-tls
backend. This does not mean the version
isn’t supported, just that it can’t be set as a minimum due to
technical limitations.
Optional
This requires the optional default-tls
, native-tls
, or rustls-tls(-...)
feature to be enabled.
Set the maximum allowed TLS version for connections.
By default there’s no maximum.
Errors
A value of tls::Version::TLS_1_3
will cause an error with the
native-tls
/default-tls
backend. This does not mean the version
isn’t supported, just that it can’t be set as a maximum due to
technical limitations.
Optional
This requires the optional default-tls
, native-tls
, or rustls-tls(-...)
feature to be enabled.
Disables the trust-dns async resolver.
This method exists even if the optional trust-dns
feature is not enabled.
This can be used to ensure a Client
doesn’t use the trust-dns async resolver
even if another dependency were to enable the optional trust-dns
feature.
Restrict the Client to be used with HTTPS only requests.
Defaults to false.
Override DNS resolution for specific domains to particular IP addresses.
Warning
Since the DNS protocol has no notion of ports, if you wish to send traffic to a particular port you must include this port in the URL itself, any port in the overridden addr will be ignored and traffic sent to the conventional port for the given scheme (e.g. 80 for http).
Trait Implementations
Performs the conversion.
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
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more