Pull a typed value out of an incoming request. Implementors decide (a) how to extract, (b) what HTTP status to return when extraction fails (encoded as a "<status>:<msg>" prefix on the err string).
pub struct ApiKey { pub key: string }
impl FromRequest for ApiKey {
fn from_request(req: *HttpRequest) -> !ApiKey {
let v: string = req.header("X-Api-Key");
if v.len() == 0 { return err("401:missing X-Api-Key"); }
return ok(ApiKey { key: v });
}
}
Now usable as a handler param:
@handler fn h(key: ApiKey) -> HttpResponse { ... }