Can I avoid this .route()
repetition handling both GET and POST with the same handler
#1304
Answered
by
jplatte
frederikhors
asked this question in
Q&A
-
First of all let me thank you all for this awesome project! I'm almost crying with joy at how good it is to use it! Now the question (I'm still studying Rust and Axum... sorry):I'm using this code and I want to handle both let mut app = Router::new()
.route("/", get(handler))
// Can I avoid this `.route()` repetition handling both GET and POST with the same handler
.route(
format!("{}/*key", &reverse_proxy_url).as_str(),
get(reverse_proxy_handler),
)
.route(
format!("{}/*key", &reverse_proxy_url).as_str(),
post(reverse_proxy_handler),
); Second question:Is there a better way than the below? format!("{}/*key", &reverse_proxy_url).as_str(), |
Beta Was this translation helpful? Give feedback.
Answered by
jplatte
Aug 23, 2022
Replies: 1 comment 3 replies
-
Yes, you can do that using routing::on, or by using the chained HTTP method methods on use axum::{
routing::on,
Router,
routing::MethodFilter,
};
// routing::on
let app = Router::new()
.route("/", get(handler))
.route(
format!("{}/*key", &reverse_proxy_url).as_str(),
on(MethodFilter::GET | MethodFilter::POST, handler),
);
// MethodRouter::post
let app = Router::new()
.route("/", get(handler))
.route(
format!("{}/*key", &reverse_proxy_url).as_str(),
get(handler).post(handler),
); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
jplatte
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, you can do that using routing::on, or by using the chained HTTP method methods on
MethodRouter
: