Struct attohttpc::RequestBuilder
source · [−]pub struct RequestBuilder<B = Empty> { /* private fields */ }
Expand description
RequestBuilder
is the main way of building requests.
You can create a RequestBuilder
using the new
or try_new
method, but the recommended way
or use one of the simpler constructors available in the crate root or on the Session
struct,
such as get
, post
, etc.
Implementations
sourceimpl RequestBuilder
impl RequestBuilder
sourceimpl<B> RequestBuilder<B>
impl<B> RequestBuilder<B>
sourcepub fn param<K, V>(self, key: K, value: V) -> Self where
K: AsRef<str>,
V: ToString,
pub fn param<K, V>(self, key: K, value: V) -> Self where
K: AsRef<str>,
V: ToString,
Associate a query string parameter to the given value.
The same key can be used multiple times.
sourcepub fn params<P, K, V>(self, pairs: P) -> Self where
P: IntoIterator,
P::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: ToString,
pub fn params<P, K, V>(self, pairs: P) -> Self where
P: IntoIterator,
P::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: ToString,
Associated a list of pairs to query parameters.
The same key can be used multiple times.
Example
attohttpc::get("http://foo.bar").params(&[("p1", "v1"), ("p2", "v2")]);
sourcepub fn bearer_auth(self, token: impl Into<String>) -> Self
pub fn bearer_auth(self, token: impl Into<String>) -> Self
Enable HTTP bearer authentication.
sourcepub fn body<B1: Body>(self, body: B1) -> RequestBuilder<B1>
pub fn body<B1: Body>(self, body: B1) -> RequestBuilder<B1>
Set the body of this request.
The BodyKind enum and Body trait determine how to implement custom request body types.
sourcepub fn text<B1: AsRef<str>>(self, body: B1) -> RequestBuilder<Text<B1>>
pub fn text<B1: AsRef<str>>(self, body: B1) -> RequestBuilder<Text<B1>>
Set the body of this request to be text.
If the Content-Type
header is unset, it will be set to text/plain
and the carset to UTF-8.
sourcepub fn bytes<B1: AsRef<[u8]>>(self, body: B1) -> RequestBuilder<Bytes<B1>>
pub fn bytes<B1: AsRef<[u8]>>(self, body: B1) -> RequestBuilder<Bytes<B1>>
Set the body of this request to be bytes.
If the Content-Type
header is unset, it will be set to application/octet-stream
.
sourcepub fn file(self, body: File) -> RequestBuilder<File>
pub fn file(self, body: File) -> RequestBuilder<File>
Set the body of this request using a local file.
If the Content-Type
header is unset, it will be set to application/octet-stream
.
sourcepub fn form<T: Serialize>(
self,
value: &T
) -> Result<RequestBuilder<Bytes<Vec<u8>>>>
pub fn form<T: Serialize>(
self,
value: &T
) -> Result<RequestBuilder<Bytes<Vec<u8>>>>
Set the body of this request to be the URL-encoded representation of the given object.
If the Content-Type
header is unset, it will be set to application/x-www-form-urlencoded
.
sourcepub fn header<H, V>(self, header: H, value: V) -> Self where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
pub fn header<H, V>(self, header: H, value: V) -> Self where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
Modify a header for this request.
If the header is already present, the value will be replaced. If you wish to append a new header,
use header_append
.
Panics
This method will panic if the value is invalid.
sourcepub fn header_append<H, V>(self, header: H, value: V) -> Self where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
pub fn header_append<H, V>(self, header: H, value: V) -> Self where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
Append a new header for this request.
The new header is always appended to the request, even if the header already exists.
Panics
This method will panic if the value is invalid.
sourcepub fn try_header<H, V>(self, header: H, value: V) -> Result<Self> where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
pub fn try_header<H, V>(self, header: H, value: V) -> Result<Self> where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
Modify a header for this request.
If the header is already present, the value will be replaced. If you wish to append a new header,
use header_append
.
sourcepub fn try_header_append<H, V>(self, header: H, value: V) -> Result<Self> where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
pub fn try_header_append<H, V>(self, header: H, value: V) -> Result<Self> where
H: IntoHeaderName,
V: TryInto<HeaderValue>,
Error: From<V::Error>,
Append a new header to this request.
The new header is always appended to the request, even if the header already exists.
sourcepub fn max_headers(self, max_headers: usize) -> Self
pub fn max_headers(self, max_headers: usize) -> Self
Set the maximum number of headers accepted in responses to this request.
The default is 100.
sourcepub fn max_redirections(self, max_redirections: u32) -> Self
pub fn max_redirections(self, max_redirections: u32) -> Self
Set the maximum number of redirections this request can perform.
The default is 5.
sourcepub fn follow_redirects(self, follow_redirects: bool) -> Self
pub fn follow_redirects(self, follow_redirects: bool) -> Self
Sets if this request should follow redirects, 3xx codes.
This value defaults to true.
sourcepub fn connect_timeout(self, duration: Duration) -> Self
pub fn connect_timeout(self, duration: Duration) -> Self
Sets a connect timeout for this request.
The default is 30 seconds.
sourcepub fn read_timeout(self, duration: Duration) -> Self
pub fn read_timeout(self, duration: Duration) -> Self
Sets a read timeout for this request.
The default is 30 seconds.
sourcepub fn timeout(self, duration: Duration) -> Self
pub fn timeout(self, duration: Duration) -> Self
Sets a timeout for the whole request.
Applies after a TCP connection is established. Defaults to no timeout.
sourcepub fn proxy_settings(self, settings: ProxySettings) -> Self
pub fn proxy_settings(self, settings: ProxySettings) -> Self
Sets the proxy settigns for this request.
If left untouched, the defaults are to use system proxy settings found in environment variables.
sourcepub fn allow_compression(self, allow_compression: bool) -> Self
pub fn allow_compression(self, allow_compression: bool) -> Self
Sets if this request will announce that it accepts compression.
This value defaults to true. Note that this only lets the browser know that this request supports compression, the server might choose not to compress the content.
sourcepub fn danger_accept_invalid_certs(self, accept_invalid_certs: bool) -> Self
pub fn danger_accept_invalid_certs(self, accept_invalid_certs: bool) -> Self
Sets if this request will accept invalid TLS certificates.
Accepting invalid certificates implies that invalid hostnames are accepted as well.
The default value is false
.
Danger
Use this setting with care. This will accept any TLS certificate valid or not. If you are using self signed certificates, it is much safer to add their root CA to the list of trusted root CAs by your system.
sourcepub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostnames: bool
) -> Self
pub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostnames: bool
) -> Self
Sets if this request will accept an invalid hostname in a TLS certificate.
The default value is false
.
Danger
Use this setting with care. This will accept TLS certificates that do not match the hostname.
sourcepub fn add_root_certificate(self, cert: Certificate) -> Self
pub fn add_root_certificate(self, cert: Certificate) -> Self
Adds a root certificate that will be trusted.
sourceimpl<B: Body> RequestBuilder<B>
impl<B: Body> RequestBuilder<B>
sourcepub fn prepare(self) -> PreparedRequest<B>
pub fn prepare(self) -> PreparedRequest<B>
Create a PreparedRequest
from this RequestBuilder
.
Panics
Will panic if an error occurs trying to prepare the request. It shouldn’t happen.
sourcepub fn try_prepare(self) -> Result<PreparedRequest<B>>
pub fn try_prepare(self) -> Result<PreparedRequest<B>>
Create a PreparedRequest
from this RequestBuilder
.
sourceimpl<B> RequestBuilder<B>
impl<B> RequestBuilder<B>
sourcepub fn inspect(&mut self) -> RequestInspector<'_, B>
pub fn inspect(&mut self) -> RequestInspector<'_, B>
Inspect the properties of this request
Trait Implementations
Auto Trait Implementations
impl<B> RefUnwindSafe for RequestBuilder<B> where
B: RefUnwindSafe,
impl<B> Send for RequestBuilder<B> where
B: Send,
impl<B> Sync for RequestBuilder<B> where
B: Sync,
impl<B> Unpin for RequestBuilder<B> where
B: Unpin,
impl<B> UnwindSafe for RequestBuilder<B> where
B: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more