Trait hreq::RequestExt[][src]

pub trait RequestExt {
    fn header(&self, key: &str) -> Option<&str>;
fn header_as<T: FromStr>(&self, key: &str) -> Option<T>;
fn send(self) -> ResponseFuture

Notable traits for ResponseFuture

impl Future for ResponseFuture type Output = Result<Response<Body>, Error>;
; }

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>[src]

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");

fn header_as<T: FromStr>(&self, key: &str) -> Option<T>[src]

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);

fn send(self) -> ResponseFuture

Notable traits for ResponseFuture

impl Future for ResponseFuture type Output = Result<Response<Body>, Error>;
[src]

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();
Loading content...

Implementations on Foreign Types

impl<B: Into<Body> + Send> RequestExt for Request<B>[src]

Loading content...

Implementors

Loading content...