Body Limit
BodyLimitMiddleware enforces a maximum request body size for specific routes. This is useful when different endpoints need different size limits.
Basic Usage
Section titled “Basic Usage”// Limit uploads to 10 MBserver.post("/upload", BodyLimitMiddleware(10 * 1024 * 1024), uploadHandler)
// Limit JSON bodies to 1 MBserver.post("/api/data", BodyLimitMiddleware(1 * 1024 * 1024), dataHandler)With Custom Timeout
Section titled “With Custom Timeout”// 5 MB limit, 60 second timeoutserver.post("/upload", BodyLimitMiddleware(5 * 1024 * 1024, timeoutMs = 60000), uploadHandler,)How It Works
Section titled “How It Works”The middleware sets the maxBodySize and optionally bodyTimeout values on the request context. When the request body is read (via request.body, request.text, request.json, or request.form), the body reader enforces these limits.
If the body exceeds the limit, the body read fails with an error.
Server Defaults
Section titled “Server Defaults”Set the defaults for all requests on ServerConfig:
val server = Server(ServerConfig( maxBodySize = 50 * 1024 * 1024, // 50 MB (default) bodyTimeout = 30000, // 30 seconds (default)))BodyLimitMiddleware overrides these defaults on a per-route basis.