Middleware that sets response header based on request header #1131
-
I would like to implement a middleware that sets a response header to the identical value as the one received in the request. Is there an easier way to achieve this than the one discussed under the scope of general state sharing here? |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Jun 30, 2022
Replies: 1 comment 1 reply
-
You can use https://docs.rs/tower-http/latest/tower_http/propagate_header/index.html. Otherwise you can do this: async fn propagate_header<B>(req: Request<B>, next: Next<B>) -> Response {
let header = req.headers().get("...").unwrap();
let mut res = next.run(req).await;
res.headers_mut().insert("key", header);
res
}
Router::new().route(...).middleware(axum::middleware::from_fn(propagate_header)); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
davidpdrsn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use https://docs.rs/tower-http/latest/tower_http/propagate_header/index.html.
Otherwise you can do this: