Skip to content

Body Limit ​

Added: v1.0.0

Request body size limiter. Rejects requests with bodies larger than the configured limit before they reach your handler.

Usage ​

go
r.Use(middleware.BodyLimit("1M"))

Custom Limit ​

go
r.Use(middleware.BodyLimit("500K"))

With Config ​

go
r.Use(middleware.BodyLimitWithConfig(middleware.BodyLimitConfig{
    Limit:   "10M",
    Skipper: zen.SkipPaths("/upload"),
}))

Or start from defaults and modify:

go
config := middleware.DefaultBodyLimitConfig()
config.Limit = "10M"
config.Skipper = zen.SkipPaths("/upload")

r.Use(middleware.BodyLimitWithConfig(config))

Options ​

OptionDefaultDescription
Limit"2M"Max body size - string ("500K", "10M", "1G"), int, or int64
SkippernilSkip body limiting for matching requests

How It Works ​

  • Wraps c.Request.Body with a http.MaxBytesReader
  • If the body exceeds the limit, the reader returns an error on read
  • The middleware returns 413 Request Entity Too Large

Supported Units ​

  • B - bytes
  • K - kilobytes
  • M - megabytes
  • G - gigabytes

Examples: "500B", "2K", "10M", "1G"

Released under the MIT License.