Skip to content

how to share data between the request and response? #236

Answered by davidpdrsn
kyle-mccarthy asked this question in Q&A
Discussion options

You must be logged in to vote

Awesome sequence diagram! You don't see a lot of those these days 😅

This is possible but arguably not as easy as it could be. The problem is that request extensions are not automatically propagated to response extensions. You could write a middleware that does that and then combine it with AsyncFilterLayer and AndThenLayer but I think the best solution is to write a single middleware that does what you need. Something like this:

use axum::{
    extract::Extension,
    handler::get,
    http::{Request, Response},
    Router,
};
use std::{
    future::Future,
    net::SocketAddr,
    pin::Pin,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    task::{Context, Poll},
};

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kyle-mccarthy
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