Skip to main content

Request

Struct Request 

Source
pub struct Request { /* private fields */ }
Expand description

An HTTP request.

Generally created by the bitreq::get-style functions, corresponding to the HTTP method we want to use.

§Example

let request = bitreq::post("http://example.com");

After creating the request, you would generally call send or send_lazy on it, as it doesn’t do much on its own.

Implementations§

Source§

impl Request

Source

pub fn new<T: Into<URL>>(method: Method, url: T) -> Request

Creates a new HTTP Request.

This is only the request’s data, it is not sent yet. For sending the request, see send.

The resource part of the URL will be encoded. Any URL special characters (e.g. &, #, =) are not encoded as they are assumed to be meaningful parameters etc.

Source

pub fn with_headers<T, K, V>(self, headers: T) -> Request
where T: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

Add headers to the request this is called on. Use this function to add headers to your requests.

Source

pub fn with_header<T: Into<String>, U: Into<String>>( self, key: T, value: U, ) -> Request

Adds a header to the request this is called on. Use this function to add headers to your requests.

Source

pub fn with_body<T: Into<Vec<u8>>>(self, body: T) -> Request

Sets the request body.

Source

pub fn with_param<T: Into<String>, U: Into<String>>( self, key: T, value: U, ) -> Request

Adds given key and value as query parameter to request url (resource).

The key and value are percent-encoded when the request is sent.

Source

pub fn with_json<T: Serialize>(self, body: &T) -> Result<Request, Error>

Converts given argument to JSON and sets it as body.

§Errors

Returns SerdeJsonError if Serde runs into a problem when converting body into a string.

Source

pub fn with_timeout(self, timeout: Duration) -> Request

Sets the request timeout.

Source

pub fn with_max_redirects(self, max_redirects: usize) -> Request

Sets the max redirects we follow until giving up. 100 by default.

Warning: setting this to a very high number, such as 1000, may cause a stack overflow if that many redirects are followed. If you have a use for so many redirects that the stack overflow becomes a problem, please open an issue.

Source

pub fn with_max_headers_size<S: Into<Option<usize>>>( self, max_headers_size: S, ) -> Request

Sets the maximum size of all the headers this request will accept.

If this limit is passed, the request will close the connection and return an Error::HeadersOverflow error.

The maximum length is counted in bytes, including line-endings and other whitespace. Both normal and trailing headers count towards this cap.

None disables the cap, and may cause the program to use any amount of memory if the server responds with a lot of headers (or an infinite amount). The default is 256KiB.

Source

pub fn with_max_status_line_length<S: Into<Option<usize>>>( self, max_status_line_len: S, ) -> Request

Sets the maximum length of the status line this request will accept.

If this limit is passed, the request will close the connection and return an Error::StatusLineOverflow error.

The maximum length is counted in bytes, including the line-ending \r\n.

None disables the cap, and may cause the program to use any amount of memory if the server responds with a long (or infinite) status line. The default is 64 KiB.

Source

pub fn with_max_body_size<S: Into<Option<usize>>>( self, max_body_size: S, ) -> Request

Sets the maximum size of the response body this request will accept.

If this limit is passed, the request will close the connection and return an Error::BodyOverflow error.

The maximum size is counted in bytes.

None disables the cap, and may cause the program to use any amount of memory if the server responds with a large (or infinite) body.

The default is 1 GiB, which is likely to cause an out-of-memory condition in many cases so setting this manually is recommended when talking to untrusted servers.

Source

pub fn with_proxy(self, proxy: Proxy) -> Request

Sets the proxy to use.

Source

pub fn with_pipelining(self) -> Request

Enables HTTP request pipelining for this request.

Note that because pipelined requests may be replayed in case of failure, you should only set this on idempotent requests.

This is only used if the request is sent using a Client and an existing connection to the same server with the same proxy exists.

Source

pub fn send(self) -> Result<Response, Error>

Sends this request to the host.

§Errors

Returns Err if we run into an error while sending the request, or receiving/parsing the response. The specific error is described in the Err, and it can be any bitreq::Error except InvalidUtf8InBody.

Source

pub fn send_lazy(self) -> Result<ResponseLazy, Error>

Sends this request to the host, loaded lazily.

§Errors

See send.

Source

pub async fn send_async(self) -> Result<Response, Error>

Sends this request to the host asynchronously.

§Errors

Returns Err if we run into an error while sending the request, or receiving/parsing the response. The specific error is described in the Err, and it can be any bitreq::Error except InvalidUtf8InBody.

Source

pub async fn send_lazy_async(self) -> Result<ResponseLazy, Error>

Sends this request to the host asynchronously, “loaded lazily”.

Note that due to API limitations the response is not actually loaded lazily - it is loaded immediately and then can be re-read from the response. In a future version an AsyncResponseLazy will be added and lazy loading support will be added.

Until then, you should use Self::send_async.

§Errors

See send_async.

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Request

Source§

impl PartialEq for Request

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl RequestExt for Request

Source§

fn send_async_with_client( self, client: &Client, ) -> impl Future<Output = Result<Response, Error>>

Sends this request asynchronously using the provided client’s connection pool.
Source§

impl StructuralPartialEq for Request

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.