How to get HTTP Headers? (newbie both in Rust & Rocket) #2041
-
Sorry for the stupid question I'm used to frameworks in other languages where I can just do something resembling this:
I could not find something built-in in Rocket 0.5.0-rc1 to do this Do I really have to do this kind of boilerplate?
As I wrote in the titile I'm a newbie both in Rust & Rocket so that code might not be very good, sorry about that |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
In your use case, Request Guards are the answer to handle the validation process. For more information about request processing and guards: You should validate HTTP Headers in the request guard as below: #[rocket::async_trait]
impl<'r> FromRequest<'r> for Authenticated {
type Error = ApiError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let res = header_bearer_validation(req).await;
match res {
Err(e) => request::Outcome::Failure((
Status::from_code(e.status_code).unwrap_or(Status::InternalServerError),
e,
)),
Ok(authenticated) => request::Outcome::Success(authenticated),
}
}
} B. r |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your answer. But if I just want ot have a look at a header (for example I just want to log it) or perform a trivial process on it (for example if http_referer == "path" then do this else do that), do I have to write a request guard every time for every header (apart for the one supported out of the box)? |
Beta Was this translation helpful? Give feedback.
Thanks a lot for your answer.
That's what I ended up doing for this case of Basic auth
But if I just want ot have a look at a header (for example I just want to log it) or perform a trivial process on it (for example if http_referer == "path" then do this else do that), do I have to write a request guard every time for every header (apart for the one supported out of the box)?