Struct axum::extract::DefaultBodyLimit

source ·
pub struct DefaultBodyLimit { /* private fields */ }
Expand description

Layer for configuring the default request body limit.

For security reasons, Bytes will, by default, not accept bodies larger than 2MB. This also applies to extractors that uses Bytes internally such as String, Json, and Form.

This middleware provides ways to configure that.

Note that if an extractor consumes the body directly with Body::data, or similar, the default limit is not applied.

§Difference between DefaultBodyLimit and RequestBodyLimit

DefaultBodyLimit and RequestBodyLimit serve similar functions but in different ways.

DefaultBodyLimit is local in that it only applies to FromRequest implementations that explicitly apply it (or call another extractor that does). You can apply the limit with RequestExt::with_limited_body or RequestExt::into_limited_body

RequestBodyLimit is applied globally to all requests, regardless of which extractors are used or how the body is consumed.

DefaultBodyLimit is also easier to integrate into an existing setup since it doesn’t change the request body type:

use axum::{
    Router,
    routing::post,
    body::Body,
    extract::{DefaultBodyLimit, RawBody},
    http::Request,
};

let app = Router::new()
    .route(
        "/",
        // even with `DefaultBodyLimit` the request body is still just `Body`
        post(|request: Request<Body>| async {}),
    )
    .layer(DefaultBodyLimit::max(1024));
use axum::{Router, routing::post, body::Body, extract::RawBody, http::Request};
use tower_http::limit::RequestBodyLimitLayer;
use http_body::Limited;

let app = Router::new()
    .route(
        "/",
        // `RequestBodyLimitLayer` changes the request body type to `Limited<Body>`
        // extracting a different body type wont work
        post(|request: Request<Limited<Body>>| async {}),
    )
    .layer(RequestBodyLimitLayer::new(1024));

In general using DefaultBodyLimit is recommended but if you need to use third party extractors and want to sure a limit is also applied there then RequestBodyLimit should be used.

Implementations§

source§

impl DefaultBodyLimit

source

pub fn disable() -> DefaultBodyLimit

Disable the default request body limit.

This must be used to receive bodies larger than the default limit of 2MB using Bytes or an extractor built on it such as String, Json, Form.

Note that if you’re accepting data from untrusted remotes it is recommend to add your own limit such as [tower_http::limit].

§Example
use axum::{
    Router,
    routing::get,
    body::{Bytes, Body},
    extract::DefaultBodyLimit,
};
use tower_http::limit::RequestBodyLimitLayer;
use http_body::Limited;

let app: Router<(), Limited<Body>> = Router::new()
    .route("/", get(|body: Bytes| async {}))
    // Disable the default limit
    .layer(DefaultBodyLimit::disable())
    // Set a different limit
    .layer(RequestBodyLimitLayer::new(10 * 1000 * 1000));
source

pub fn max(limit: usize) -> DefaultBodyLimit

Set the default request body limit.

By default the limit of request body sizes that Bytes::from_request (and other extractors built on top of it such as String, Json, and Form) is 2MB. This method can be used to change that limit.

§Example
use axum::{
    Router,
    routing::get,
    body::{Bytes, Body},
    extract::DefaultBodyLimit,
};
use tower_http::limit::RequestBodyLimitLayer;
use http_body::Limited;

let app: Router<(), Limited<Body>> = Router::new()
    .route("/", get(|body: Bytes| async {}))
    // Replace the default of 2MB with 1024 bytes.
    .layer(DefaultBodyLimit::max(1024));

Trait Implementations§

source§

impl Clone for DefaultBodyLimit

source§

fn clone(&self) -> DefaultBodyLimit

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for DefaultBodyLimit

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<S> Layer<S> for DefaultBodyLimit

source§

type Service = DefaultBodyLimitService<S>

The wrapped service
source§

fn layer(&self, inner: S) -> <DefaultBodyLimit as Layer<S>>::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more