I want the request headers to have the correct api key, but I can’t get the api key from the request headers, my current Rust code:
fn app() -> Router<Arc<AppState>> {
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods(vec![Method::GET])
.allow_headers(Any);
Router::new()
.route("/", get(handler))
.layer(cors)
.layer(middleware::from_fn(auth_middleware))
}
async fn auth_middleware(req: Request, next: Next) -> Response {
if let Some(api_key) = req.headers().get("x-api-key") {
println!("{:?}", api_key);
if api_key == "Test" {
return next.run(req).await;
}
}
(StatusCode::UNAUTHORIZED, "Unauthorized").into_response()
}
req.headers().get("x-api-key")
always returns None
I have tried sending requests from the frontend many times with the following code:
const API_URL = "http://127.0.0.1:8000/";
const fetchFromApi = (query, filters) => {
const headers = {
"x-api-key": "Test",
};
const params = {
query: query,
fuzzy: filters.fuzzy,
text: false,
};
const config = {
headers: headers,
params: params,
};
return axios.get(API_URL, config).then(response => response.data["items"]);
};
params is correct, headers looks fine but still doesn’t solve the problem
What is the problem?