Skip to content

Request limits and bodies

Global and per-route body size caps, streaming bodies, and when Flare buffers versus streams.

AI generated, pending review Updated 10 days ago · Flare 0.3

Flare enforces body size limits and chooses buffering versus streaming based on your contract descriptor. Oversized bodies return 413 ContentTooLarge. Malformed JSON returns 400.

The global cap is host.maxBodyBytes in flare.json (default 2 MiB). See flare.json reference.

{
"host": {
"maxBodyBytes": 2097152
}
}

Override at runtime: FLARE__HOST__MAXBODYBYTES=4194304.

Add maxBodyBytes on the route descriptor (inline field or httpContract entry):

import { httpContract } from "@flare-ts/core";
import { model, str } from "@flare-ts/lib/schema";
class FeedbackBody extends model({ message: str.min(1) }) {}
const FeedbackContract = httpContract({
submit: {
body: FeedbackBody,
maxBodyBytes: 51200,
},
});

Bodies over the limit return 413 with { error: "ContentTooLarge", code: 413, detail: { maxBytes } }.

When the descriptor declares a body schema or model(), Flare buffers and validates JSON after before middleware. Read the result from scope.input.body (inline) or ctx.extract(entry).body (controller). An empty or missing JSON body becomes null.

Declare body: stream (import stream from @flare-ts/core) to skip JSON buffering. The inbound stream attaches before before middleware runs.

import { FlareHost, FlareResponse, stream } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.post("/upload", { body: stream, maxBodyBytes: 50 * 1024 * 1024 }, async (ctx) => {
let bytes = 0;
for await (const chunk of ctx.req.stream()) {
bytes += chunk.byteLength;
}
return new FlareResponse(200, { bytes });
});
const app = host.build();
app.run();

Size is enforced while iterating chunks, not only up front.

One read strategy per request. stream() throws if buffer(), text(), or json() already started.

Without a body in the descriptor, Flare does not parse JSON. Use ctx.req.buffer(), text(), json(), or stream() directly. See Requests.

InputStatusBody shape
Route params400Invalid route parameters message
Query string400Invalid query parameters message
JSON body (schema failure)400{ error, details: [ field errors ] }
Body too large413ContentTooLarge with maxBytes

These bypass host.http.error().