[−][src]Trait hreq::ResponseExt
Extends http::request::Response
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 res = Request::get("https://www.google.com") .call().block().unwrap(); let x_frame_opts = res.header("x-frame-options").unwrap(); assert_eq!(x_frame_opts, "SAMEORIGIN");
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 res = Request::get("https://my-api") .call().block().unwrap(); let req_id: u64 = res.header_as("x-req-id").unwrap();
fn status_code(&self) -> u16
Get the response status code as a u16
These two are equivalent:
use hreq::prelude::*; let res = Request::get("https://www.google.com") .call().block().unwrap(); assert_eq!(res.status_code(), 200); assert_eq!(res.status().as_u16(), 200);