pub fn header<T: FromStr + Send + 'static>(
    name: &'static str
) -> impl Filter<Extract = (T,), Error = Rejection> + Copy
Expand description

Create a Filter that tries to parse the specified header.

This Filter will look for a header with supplied name, and try to parse to a T, otherwise rejects the request.

Example

use std::net::SocketAddr;

// Parse `content-length: 100` as a `u64`
let content_length = warp::header::<u64>("content-length");

// Parse `host: 127.0.0.1:8080` as a `SocketAddr
let local_host = warp::header::<SocketAddr>("host");

// Parse `foo: bar` into a `String`
let foo = warp::header::<String>("foo");
Examples found in repository?
examples/rejections.rs (line 43)
42
43
44
45
46
47
48
49
50
fn div_by() -> impl Filter<Extract = (NonZeroU16,), Error = Rejection> + Copy {
    warp::header::<u16>("div-by").and_then(|n: u16| async move {
        if let Some(denom) = NonZeroU16::new(n) {
            Ok(denom)
        } else {
            Err(reject::custom(DivideByZero))
        }
    })
}
More examples
Hide additional examples
examples/headers.rs (line 17)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
async fn main() {
    pretty_env_logger::init();

    // For this example, we assume no DNS was used,
    // so the Host header should be an address.
    let host = warp::header::<SocketAddr>("host");

    // Match when we get `accept: */*` exactly.
    let accept_stars = warp::header::exact("accept", "*/*");

    let routes = host
        .and(accept_stars)
        .map(|addr| format!("accepting stars on {}", addr));

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}