I am using actix-web to authenticate a user in a REST api. The endpoint for authenticating a user sets the "Authorization" header of the response object to a generated token.
async fn signin(request: web::Json<UserAndPw>) -> impl Responder {
let token = String::from("thisisatest");
HttpResponse::Ok()
.header(AUTHORIZATION, HeaderValue::from_static(&token))
.json(ApiResponse {...})
}
This is error occurs when compiled:
.header(AUTHORIZATION, HeaderValue::from_static(&test_token))
-------------------------^^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `test_token` is borrowed for `'static`
>Solution :
In your case token is a local variable and a reference to it (&token) is definitely not with a static lifetime. The compiler error explains it and shows it to you.
You probably want to create a HeaderValue instance some other way, not via from_static. For example like this: HeaderValue::from_str(&token).