Body Limit
Reject request bodies larger than a configured size.
Body Limit
middleware.BodyLimit rejects requests whose body exceeds a byte limit.
app.Use(middleware.BodyLimit(1 << 20))The example allows up to 1 MiB. A larger request returns:
HTTP/1.1 413 Request Entity Too Large
Content-Type: text/plainThe body is request body too large.
What it checks
The middleware checks both:
- the declared
Content-Length, - the actual request body length.
The declared length catches oversized requests before the handler reads the
body. The actual length covers bodies without a usable Content-Length.
BodyLimit panics when the limit is not positive.
Ordering
Place it before handlers that parse or store request data:
app.Use(
middleware.SecurityHeaders(),
middleware.BodyLimit(2 << 20),
middleware.Logger(),
)Limits
fasthttp reads the request body before the handler chain runs. BodyLimit
prevents application processing, but it does not prevent the server from
receiving the bytes. Set a request size limit at the reverse proxy as well:
client_max_body_size 2m;Use a smaller route-specific limit for upload endpoints that accept only small files. Put the global limit first, then add a stricter handler check for specific routes.
Difference from upload limits
BodyLimit applies to every request that runs the middleware. File upload
handlers should also validate FileHeader.Size, file type, and file count
before saving.

Ply