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
impl Request
Sourcepub fn new<T: Into<URL>>(method: Method, url: T) -> Request
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.
Sourcepub fn with_headers<T, K, V>(self, headers: T) -> Request
pub fn with_headers<T, K, V>(self, headers: T) -> Request
Add headers to the request this is called on. Use this function to add headers to your requests.
Sourcepub fn with_header<T: Into<String>, U: Into<String>>(
self,
key: T,
value: U,
) -> Request
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.
Sourcepub fn with_param<T: Into<String>, U: Into<String>>(
self,
key: T,
value: U,
) -> Request
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.
Sourcepub fn with_json<T: Serialize>(self, body: &T) -> Result<Request, Error>
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.
Sourcepub fn with_timeout(self, timeout: Duration) -> Request
pub fn with_timeout(self, timeout: Duration) -> Request
Sets the request timeout.
Sourcepub fn with_max_redirects(self, max_redirects: usize) -> Request
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.
Sourcepub fn with_max_headers_size<S: Into<Option<usize>>>(
self,
max_headers_size: S,
) -> Request
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.
Sourcepub fn with_max_status_line_length<S: Into<Option<usize>>>(
self,
max_status_line_len: S,
) -> Request
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.
Sourcepub fn with_max_body_size<S: Into<Option<usize>>>(
self,
max_body_size: S,
) -> Request
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.
Sourcepub fn with_proxy(self, proxy: Proxy) -> Request
pub fn with_proxy(self, proxy: Proxy) -> Request
Sets the proxy to use.
Sourcepub fn with_pipelining(self) -> Request
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.
Sourcepub fn send(self) -> Result<Response, Error>
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.
Sourcepub fn send_lazy(self) -> Result<ResponseLazy, Error>
pub fn send_lazy(self) -> Result<ResponseLazy, Error>
Sourcepub async fn send_async(self) -> Result<Response, Error>
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.
Sourcepub async fn send_lazy_async(self) -> Result<ResponseLazy, Error>
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.