Warp: How do I extract an optional header?

Created on 2 Mar 2019  路  4Comments  路  Source: seanmonstar/warp

I have this right now:

warp::header::<String>("accept-encoding").or(warp::any().map(|| String::new())).unify()

Is there a way to extract an optional header like:

warp::header::<Option<String>>("accept-encoding")

Most helpful comment

@mattdamon108 That'd just use an or filter:

let default_auth = warp::any().map(|| {
    // something default
});

let auth = warp::header("authorization")
    .map(|token: String| {
        // something with token
    })
    .or(default_auth)
    .unify();

All 4 comments

I think adding a general optional method to the Filter trait may be a nice addition. It'd be sort of like Result::ok()...

@seanmonstar First of all, I really really like your work.

I have a similar issue.

warp::any().and(warp::header::<String>("authorization")
    .map(|token: String| { // ...something to do with token }

This filter composition rejects all the request without authorization in header. But, I'd like to modify it with optional condition if request doesn't contain a authorization in header. How can I achieve it?

I'm sorry I'm very new to Rust, can you show me a example code for this?

@mattdamon108 That'd just use an or filter:

let default_auth = warp::any().map(|| {
    // something default
});

let auth = warp::header("authorization")
    .map(|token: String| {
        // something with token
    })
    .or(default_auth)
    .unify();

@seanmonstar It works like charm! Thank you for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Apromixately picture Apromixately  路  3Comments

stevensonmt picture stevensonmt  路  5Comments

whitfin picture whitfin  路  3Comments

weiznich picture weiznich  路  7Comments

seanmonstar picture seanmonstar  路  3Comments