Skip to main content

Import

Usage

Options

The bodyLimit middleware accepts a BodyLimitOptions object:
number
required
The maximum body size allowed in bytes.
(c: Context) => Response | Promise<Response>
Custom error handler invoked if the specified body size is exceeded. If not provided, returns a 413 Payload Too Large response with message “Payload Too Large”.

Signature

Examples

Basic usage with size limit

Custom error handler

Global body limit

Behavior

  • Checks Content-Length header when available for early rejection
  • Follows RFC 7230: Transfer-Encoding takes precedence over Content-Length
  • For chunked transfers, streams the body and validates size incrementally
  • Throws HTTPException with 413 status when size limit is exceeded
  • Skips validation for GET and HEAD requests (no body expected)
  • Returns early if no request body is present