Skip to content

Middleware that sets response header based on request header #1131

Answered by davidpdrsn
dspicher asked this question in Q&A
Discussion options

You must be logged in to vote

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));

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dspicher
Comment options

Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants