pub trait RequestExt {
// Required methods
fn header(&self, key: &str) -> Option<&str>;
fn header_as<T: FromStr>(&self, key: &str) -> Option<T>;
fn send(self) -> ResponseFuture ⓘ;
}Expand description
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§
Sourcefn header(&self, key: &str) -> Option<&str>
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://httpbin.org/get")
.header("x-my-head", "whatnow")
.with_body(()).unwrap();
let value = req.header("x-my-head").unwrap();
assert_eq!(value, "whatnow");Sourcefn header_as<T: FromStr>(&self, key: &str) -> Option<T>
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);Sourcefn send(self) -> ResponseFuture ⓘ
fn send(self) -> ResponseFuture ⓘ
Send this request.
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://httpbin.org/get")
.with_body(()).unwrap();
req.send().block();Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.