Is there a way to load a particular extractor in middleware (I'm guessing yes)? #1870
Answered
by
davidpdrsn
thomasmost
asked this question in
Q&A
-
I know how to compose extensions in middleware, i.e. an 'admin' middleware can expect a 'session_user' extension... but is there a way to check a Query extractor in middleware? Instead of loading a Query, I am currently doing this: pub async fn current_org<B>(mut req: Request<B>, next: Next<B>) -> Result<Response, StatusCode> {
if let Some(full_path) = req.uri().path_and_query() {
let re = Regex::new(r"(.*)?org=([a-zA-Z0-9\-]*)").unwrap();
let uri_str = full_path.to_string();
if let Some(captures) = re.captures(&uri_str) {
let org_id = captures.get(2).map_or("", |m| m.as_str());
... ...but it would obviously be easier to do something like this: let Some(query) = req.extensions().get::<Query<OrgId>>() else {
return Err(
StatusCode::BAD_REQUEST
);
}; ...but this doesn't work (I've tried it). Which makes sense, since an 'extracted' Query is not a request extension, as I understand axum's model |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Mar 21, 2023
Replies: 1 comment
-
https://docs.rs/axum/latest/axum/middleware/fn.from_fn.html#running-extractors |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jplatte
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.rs/axum/latest/axum/middleware/fn.from_fn.html#running-extractors