Skip to content

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
Discussion options

You must be logged in to vote

Yes, you can do that using routing::on, or by using the chained HTTP method methods on MethodRouter:

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

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@frederikhors
Comment options

@davidpdrsn
Comment options

@frederikhors
Comment options

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