//! Run with //! //! ```not_rust //! cargo run -p example-consume-body-in-extractor-or-middleware //! ``` use axum::{ async_trait, body::{Body, Bytes}, extract::{FromRequest, Request}, http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::post, Router, }; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "example_consume_body_in_extractor_or_middleware=debug".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); let app = Router::new() .route("/", post(handler)) .layer(middleware::from_fn(print_request_body)); let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await .unwrap(); tracing::debug!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } // middleware that shows how to consume the request body upfront async fn print_request_body(request: Request, next: Next) -> Result { let request = buffer_request_body(request).await?; Ok(next.run(request).await) } // the trick is to take the request apart, buffer the body, do what you need to do, then put // the request back together async fn buffer_request_body(request: Request) -> Result { let (parts, body) = request.into_parts(); // this wont work if the body is an long running stream let bytes = hyper::body::to_bytes(body) .await .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?; do_thing_with_request_body(bytes.clone()); Ok(Request::from_parts(parts, Body::from(bytes))) } fn do_thing_with_request_body(bytes: Bytes) { tracing::debug!(body = ?bytes); } async fn handler(BufferRequestBody(body): BufferRequestBody) { tracing::debug!(?body, "handler received body"); } // extractor that shows how to consume the request body upfront struct BufferRequestBody(Bytes); // we must implement `FromRequest` (and not `FromRequestParts`) to consume the body #[async_trait] impl FromRequest for BufferRequestBody where S: Send + Sync, { type Rejection = Response; async fn from_request(req: Request, state: &S) -> Result { let body = Bytes::from_request(req, state) .await .map_err(|err| err.into_response())?; do_thing_with_request_body(body.clone()); Ok(Self(body)) } }