Hey everyone!
I wanted to ask how I can access custom headers in a normal request with actix_web 4.
The following way works, but I want to access a header like X-Auth-Stuff, which I can't do with the method below. Any ideas?
pub async fn some_handler(req: actix_web::HttpRequest) -> Result<HttpResponse, Error> {
let headers = req.headers();
if let Some(auth_header_value) = headers.get(HeaderName::from_static("x-auth-stuff")) /*note: header name must be all lower case*/ {
// do something with the auth value
} else {
// custom auth header not found
}
...
}
Please keep in mind that HeaderName::from_static only accepts lowercase letters as the header name (according to HTTP2 spec).
Also keep in mind that HeaderMap is a "multi-map", which means multiple values can be stored per key. If you want to get all values that are associated with a certain header name, use get_all method.