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
| Option | Default | Description |
|---|---|---|
Limit | "2M" | Max body size - string ("500K", "10M", "1G"), int, or int64 |
Skipper | nil | Skip body limiting for matching requests |
How It Works
- Wraps
c.Request.Bodywith ahttp.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- bytesK- kilobytesM- megabytesG- gigabytes
Examples: "500B", "2K", "10M", "1G"