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?
More 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;
}