[−][src]Trait hreq::RequestExt
Extends http::request::Request
with ergonomic extras for hreq.
These extensions are part of the primary goal of hreq to provide a "User first API".
Required methods
fn header(&self, key: &str) -> Option<&str>
Quickly read a header value as a &str
.
A header value can in theory contain any byte value 32 to 255 (inclusive), excluding
127 (DEL). That means all possible header values are not representable as a &str
.
In practice it's incredibly rare for any header value to be outside US-ASCII, which
means for the vast majority of cases &str
is fine.
This convenience methods treats header values not representable as ascii as None
.
use hreq::prelude::*; let req = Request::get("https://www.google.com") .header("x-my-head", "whatnow") .with_body(()).unwrap(); let value = req.header("x-my-head").unwrap(); assert_eq!(value, "whatnow");
fn header_as<T: FromStr>(&self, key: &str) -> Option<T>
Quickly parse a header value into something else.
Rust fabulous FromStr
trait means we can quickly parse a value into something else.
For example, if we know a header x-req-id
is supposed to have a numeric 64 bit value
and we want that number, we can do:
use hreq::prelude::*; let req = Request::get("https://my-api") .header("x-req-id", 42) .with_body(()).unwrap(); let req_id: u64 = req.header_as("x-req-id").unwrap(); assert_eq!(req_id, 42);
#[must_use]fn send<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait>> where
Self: 'async_trait,
self
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait>> where
Self: 'async_trait,
Send this request.
Note: The type signature of this function is complicated because rust doesn't yet
support the async
keyword in traits. You can think of this function as:
async fn send(self) -> Result<Response<Body>, Error>;
Creates a default configured Agent
used for this request only. The agent will
follow redirects and provide some retry-logic for idempotent request methods.
If you need connection pooling over several requests or finer grained control over
retries or redirects, instantiate an Agent
and send the request through it.
use hreq::prelude::*; let req = Request::get("https://www.google.com") .with_body(()).unwrap(); req.send().block();
Implementations on Foreign Types
impl<B: Into<Body> + Send> RequestExt for Request<B>
[src]
fn header(&self, key: &str) -> Option<&str>
[src]
fn header_as<T: FromStr>(&self, key: &str) -> Option<T>
[src]
fn send<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait>> where
Self: 'async_trait,
[src]
self
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait>> where
Self: 'async_trait,