> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/tokio-rs/axum/llms.txt
> Use this file to discover all available pages before exploring further.

# MethodRouter

> Route requests to handlers based on HTTP methods

A `Service` that accepts requests based on a `MethodFilter` and allows chaining additional handlers and services.

Whether or not `MethodRouter` implements `Service` depends on the state type it requires. A `MethodRouter` without required state (i.e., `MethodRouter<()>`) implements `Service`. If it requires state, you must call `with_state()` first.

## Top-level handler functions

<ParamField path="get" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `GET` requests to the given handler.

  Note that `get` routes will also be called for `HEAD` requests but will have the response body removed. Make sure to add explicit `HEAD` routes afterwards if needed.

  ```rust theme={null}
  use axum::{routing::get, Router};

  async fn handler() -> &'static str {
      "Hello, World!"
  }

  let app = Router::new().route("/", get(handler));
  ```
</ParamField>

<ParamField path="post" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `POST` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::post, Router};

  async fn create_user() -> &'static str {
      "User created"
  }

  let app = Router::new().route("/users", post(create_user));
  ```
</ParamField>

<ParamField path="put" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `PUT` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::put, Router};

  async fn update_user() -> &'static str {
      "User updated"
  }

  let app = Router::new().route("/users/:id", put(update_user));
  ```
</ParamField>

<ParamField path="delete" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `DELETE` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::delete, Router};

  async fn delete_user() -> &'static str {
      "User deleted"
  }

  let app = Router::new().route("/users/:id", delete(delete_user));
  ```
</ParamField>

<ParamField path="patch" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `PATCH` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::patch, Router};

  async fn patch_user() -> &'static str {
      "User patched"
  }

  let app = Router::new().route("/users/:id", patch(patch_user));
  ```
</ParamField>

<ParamField path="head" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `HEAD` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::head, Router};

  async fn head_handler() {}

  let app = Router::new().route("/", head(head_handler));
  ```
</ParamField>

<ParamField path="options" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `OPTIONS` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::options, Router};

  async fn options_handler() -> &'static str {
      "OPTIONS response"
  }

  let app = Router::new().route("/", options(options_handler));
  ```
</ParamField>

<ParamField path="trace" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `TRACE` requests to the given handler.

  ```rust theme={null}
  use axum::{routing::trace, Router};

  async fn trace_handler() {}

  let app = Router::new().route("/", trace(trace_handler));
  ```
</ParamField>

<ParamField path="connect" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route `CONNECT` requests to the given handler.

  See `MethodFilter::CONNECT` for when you'd want to use this.

  ```rust theme={null}
  use axum::{routing::connect, Router};

  async fn connect_handler() {}

  let app = Router::new().route("/", connect(connect_handler));
  ```
</ParamField>

<ParamField path="on" type="fn<H, T, S>(filter: MethodFilter, handler: H) -> MethodRouter<S, Infallible>">
  Route requests with the given method filter to the handler.

  ```rust theme={null}
  use axum::{routing::{on, MethodFilter}, Router};

  async fn handler() {}

  let app = Router::new().route("/", on(MethodFilter::POST, handler));
  ```
</ParamField>

<ParamField path="any" type="fn<H, T, S>(handler: H) -> MethodRouter<S, Infallible>">
  Route requests with any method to the given handler.

  Additional methods can still be chained to override specific methods.

  ```rust theme={null}
  use axum::{routing::any, Router};

  async fn handler() -> &'static str {
      "Handles all methods"
  }

  async fn post_handler() -> &'static str {
      "Handles POST specifically"
  }

  // POST goes to post_handler, all other methods go to handler
  let app = Router::new().route("/", any(handler).post(post_handler));
  ```
</ParamField>

## Top-level service functions

<ParamField path="get_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `GET` requests to the given service.

  Note that `get` routes will also be called for `HEAD` requests but will have the response body removed.

  ```rust theme={null}
  use axum::{extract::Request, Router, routing::get_service, body::Body};
  use http::Response;
  use std::convert::Infallible;

  let service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let app = Router::new().route("/", get_service(service));
  ```
</ParamField>

<ParamField path="post_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `POST` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="put_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `PUT` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="delete_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `DELETE` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="patch_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `PATCH` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="head_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `HEAD` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="options_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `OPTIONS` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="trace_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `TRACE` requests to the given service.

  See `get_service` for an example.
</ParamField>

<ParamField path="connect_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route `CONNECT` requests to the given service.

  See `MethodFilter::CONNECT` for when you'd want to use this, and `get_service` for an example.
</ParamField>

<ParamField path="on_service" type="fn<T, S>(filter: MethodFilter, svc: T) -> MethodRouter<S, T::Error>">
  Route requests with the given method to the service.

  ```rust theme={null}
  use axum::{
      extract::Request,
      routing::on_service,
      Router,
      body::Body,
      routing::MethodFilter,
  };
  use http::Response;
  use std::convert::Infallible;

  let service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let app = Router::new().route("/", on_service(MethodFilter::POST, service));
  ```
</ParamField>

<ParamField path="any_service" type="fn<T, S>(svc: T) -> MethodRouter<S, T::Error>">
  Route requests to the given service regardless of method.

  Additional methods can still be chained to override specific methods.

  ```rust theme={null}
  use axum::{extract::Request, Router, routing::any_service, body::Body};
  use http::Response;
  use std::convert::Infallible;

  let service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let other_service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  // POST goes to other_service, all other requests go to service
  let app = Router::new().route("/", any_service(service).post_service(other_service));
  ```
</ParamField>

## MethodRouter instance methods

<ParamField path="MethodRouter::new" type="fn() -> Self">
  Create a default `MethodRouter` that will respond with `405 Method Not Allowed` to all requests.

  ```rust theme={null}
  use axum::routing::MethodRouter;

  let method_router = MethodRouter::new();
  ```
</ParamField>

<ParamField path="on" type="fn<H, T>(self, filter: MethodFilter, handler: H) -> Self">
  Chain an additional handler that will accept requests matching the given `MethodFilter`.

  ```rust theme={null}
  use axum::{routing::get, Router, routing::MethodFilter};

  async fn handler() {}
  async fn other_handler() {}

  let app = Router::new()
      .route("/", get(handler).on(MethodFilter::DELETE, other_handler));
  ```
</ParamField>

<ParamField path="get" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `GET` requests.

  Note that `get` routes will also be called for `HEAD` requests but will have the response body removed.

  ```rust theme={null}
  use axum::{routing::post, Router};

  async fn handler() {}
  async fn other_handler() {}

  let app = Router::new()
      .route("/", post(handler).get(other_handler));
  ```
</ParamField>

<ParamField path="post" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `POST` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="put" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `PUT` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="delete" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `DELETE` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="patch" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `PATCH` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="head" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `HEAD` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="options" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `OPTIONS` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="trace" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `TRACE` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="connect" type="fn<H, T>(self, handler: H) -> Self">
  Chain an additional handler that will only accept `CONNECT` requests.

  See `MethodRouter::get` for an example.
</ParamField>

<ParamField path="on_service" type="fn<T>(self, filter: MethodFilter, svc: T) -> Self">
  Chain an additional service that will accept requests matching the given `MethodFilter`.

  ```rust theme={null}
  use axum::{
      extract::Request,
      Router,
      routing::{MethodFilter, on_service},
      body::Body,
  };
  use http::Response;
  use std::convert::Infallible;

  let service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let app = Router::new().route("/", on_service(MethodFilter::DELETE, service));
  ```
</ParamField>

<ParamField path="get_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `GET` requests.

  ```rust theme={null}
  use axum::{
      extract::Request,
      Router,
      routing::post_service,
      body::Body,
  };
  use http::Response;
  use std::convert::Infallible;

  let service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let other_service = tower::service_fn(|request: Request| async {
      Ok::<_, Infallible>(Response::new(Body::empty()))
  });

  let app = Router::new()
      .route("/", post_service(service).get_service(other_service));
  ```
</ParamField>

<ParamField path="post_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `POST` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="put_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `PUT` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="delete_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `DELETE` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="patch_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `PATCH` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="head_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `HEAD` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="options_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `OPTIONS` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="trace_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `TRACE` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="connect_service" type="fn<T>(self, svc: T) -> Self">
  Chain an additional service that will only accept `CONNECT` requests.

  See `MethodRouter::get_service` for an example.
</ParamField>

<ParamField path="fallback" type="fn<H, T>(self, handler: H) -> Self">
  Add a fallback handler to the method router.

  This handler is called when the request method doesn't match any of the configured methods.

  ```rust theme={null}
  use axum::{routing::get, Router, http::StatusCode};

  async fn get_handler() -> &'static str {
      "GET request"
  }

  async fn fallback_handler() -> (StatusCode, &'static str) {
      (StatusCode::METHOD_NOT_ALLOWED, "Method not allowed")
  }

  let app = Router::new()
      .route("/", get(get_handler).fallback(fallback_handler));
  ```
</ParamField>

<ParamField path="fallback_service" type="fn<T>(self, svc: T) -> Self">
  Add a fallback service to the method router.

  Like `fallback` but accepts a `Service` instead of a handler.

  ```rust theme={null}
  use axum::{extract::Request, routing::get, Router, body::Body};
  use http::Response;
  use std::convert::Infallible;

  let fallback_service = tower::service_fn(|_: Request| async {
      Ok::<_, Infallible>(Response::new(Body::from("Fallback")))
  });

  let app = Router::new()
      .route("/", get(|| async { "GET" }).fallback_service(fallback_service));
  ```
</ParamField>

<ParamField path="layer" type="fn<L>(self, layer: L) -> MethodRouter<S, NewError>">
  Apply a `tower::Layer` to the method router.

  The layer is applied to all methods including the fallback.

  ```rust theme={null}
  use axum::{routing::get, Router};
  use tower_http::trace::TraceLayer;

  let app = Router::new()
      .route("/", get(|| async { "Hello" }).layer(TraceLayer::new_for_http()));
  ```
</ParamField>

<ParamField path="route_layer" type="fn<L>(self, layer: L) -> Self">
  Apply a `tower::Layer` to the method router's routes, but not the fallback.

  This is useful when you want middleware to only apply to specific HTTP methods.

  ```rust theme={null}
  use axum::{routing::get, Router};
  use tower_http::compression::CompressionLayer;

  let app = Router::new()
      .route("/", 
          get(|| async { "data" })
              .route_layer(CompressionLayer::new())
              .fallback(|| async { "Not allowed" })
      );
  ```
</ParamField>

<ParamField path="merge" type="fn(self, other: Self) -> Self">
  Merge two method routers into one.

  This allows combining different method handlers. Both routers cannot have a custom fallback.

  ```rust theme={null}
  use axum::{routing::{get, post}, Router};

  let get_route = get(|| async { "GET" });
  let post_route = post(|| async { "POST" });

  let app = Router::new()
      .route("/", get_route.merge(post_route));
  ```
</ParamField>

<ParamField path="with_state" type="fn<S2>(self, state: S) -> MethodRouter<S2, E>">
  Provide the state for the method router.

  This converts a `MethodRouter<S>` into a `MethodRouter<()>` that can be used as a service.

  ```rust theme={null}
  use axum::{routing::get, extract::State};

  #[derive(Clone)]
  struct AppState {
      count: u32,
  }

  let method_router = get(|State(state): State<AppState>| async move {
      format!("Count: {}", state.count)
  }).with_state(AppState { count: 0 });
  ```
</ParamField>

<ParamField path="handle_error" type="fn<F, T>(self, f: F) -> MethodRouter<S, Infallible>">
  Apply a `HandleErrorLayer`.

  This is a convenience method for `self.layer(HandleErrorLayer::new(f))`.

  ```rust theme={null}
  use axum::{routing::get, Router, http::StatusCode};

  async fn handler() -> Result<&'static str, std::io::Error> {
      Ok("Hello")
  }

  let app = Router::new()
      .route("/", 
          get(handler).handle_error(|_err| async {
              (StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong")
          })
      );
  ```
</ParamField>

<ParamField path="method_filter" type="fn(&self) -> Option<MethodFilter>">
  Get a `MethodFilter` for the methods that this `MethodRouter` has custom code for.

  Returns `None` if the `MethodRouter` was constructed with `any` or has had a `fallback` set.

  ```rust theme={null}
  use axum::routing::{get, post, MethodFilter};

  let method_router = get(|| async {}).post(|| async {});
  let filter = method_router.method_filter();
  assert_eq!(filter, Some(MethodFilter::GET | MethodFilter::POST));
  ```
</ParamField>

<ParamField path="into_make_service" type="fn(self) -> IntoMakeService<Self>">
  Convert the method router into a `MakeService`.

  This allows you to serve a single `MethodRouter` without path-based routing.

  Only available for `MethodRouter<(), Infallible>`.

  ```rust theme={null}
  use axum::routing::get;

  let router = get(|| async { "GET" })
      .post(|| async { "POST" });

  # async {
  let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
  axum::serve(listener, router.into_make_service()).await;
  # };
  ```
</ParamField>

<ParamField path="into_make_service_with_connect_info" type="fn<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C>">
  Convert the method router into a `MakeService` which stores connection information.

  Requires the `tokio` feature. Only available for `MethodRouter<(), Infallible>`.

  ```rust theme={null}
  use axum::{routing::get, extract::ConnectInfo};
  use std::net::SocketAddr;

  async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
      format!("Hello {addr}")
  }

  let router = get(handler).post(handler);

  # async {
  let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
  axum::serve(listener, router.into_make_service_with_connect_info::<SocketAddr>()).await;
  # };
  ```
</ParamField>
