Request bodies and limits
c.body(), c.bodyJson(), c.bodyBytes(), c.req.json() and the like. The default 1 MiB limit, and raising it with bodyLimit()
import { } from "@senrijs/senri";
const = ();
.("/notes", async (, ) => {
const = await .<{ : string }>(); // read by Content-Type
return { : . };
});Ways to read
| Write | Result |
|---|---|
c.body() | A value for JSON, an object for forms, a string for text, an ArrayBuffer for anything else |
c.bodyJson() | Reads JSON whatever the Content-Type. 400 if it cannot be parsed |
c.bodyBytes() | Reads an ArrayBuffer whatever the Content-Type |
c.req.json(), text(), formData(), arrayBuffer() | The Request methods as is |
c.req.body | Read it as a stream |
A route's body schema | Read the checked value with c.valid("body") (Validation) |
c.body() returns the same result however many times you call it, so both middleware and the handler can read it.
Read as JSON or as bytes
The shape of what c.body() returns depends on the Content-Type. To decide the shape yourself, use c.bodyJson() or c.bodyBytes().
.("/notes", async (, ) => {
const = await .<{ : string }>(); // JSON, whatever the Content-Type
return { : . };
});
.("/upload", async (, ) => {
const = await .(); // always an ArrayBuffer
return { : . };
});c.bodyJson()turns invalid JSON and an empty body into 400.<T>only adds a type and does not check the value. To check it, use the route'sbodyschema- All three return the same value however many times you call them.
c.body()andc.bodyJson()can both be read on the same request, in either order
How they differ from c.req.json()
c.req.json() and c.req.arrayBuffer() are the Request methods as is.
c.bodyJson(), c.bodyBytes() | c.req.json(), c.req.arrayBuffer() | |
|---|---|---|
| Called a second time | The same value as the first time | An error (the body was already read), so 500 |
| Invalid JSON | 400 | 500 |
The default limit (1 MiB) and bodyLimit() | Apply | Apply |
The limit applies the same to both. What differs is whether the result is kept, and the error response.
Check a signature, then read
When you need the original bytes, as for a webhook signature, call c.bodyBytes() first in a middleware. c.body(), c.bodyJson() and the route's body schema then read from those bytes.
import { } from "@senrijs/senri";
.("/webhook", async (, ) => {
const = await (await .(), ...("x-signature"));
if (!) throw new (401);
return ();
});
.("/webhook", async (, ) => {
const = await .<{ : string }>(); // reads the same bytes
return { : . };
});The other order does not work. After the body was read as text or a form (by c.body(), c.bodyJson(), a schema, c.req.text() and so on), the original bytes are gone, so c.bodyBytes() fails with an error whose code is "body_bytes_after_read".
The default limit is 1 MiB
Reading a body (c.body(), c.bodyJson(), c.bodyBytes(), c.req.json() and so on) is limited to 1 MiB by default. Over it, the response is 413.
- It applies only when the body is read. Routes that do not read the body behave the same with a large body
- If Content-Length is over the limit, the request gets 413 without the body being read. A body without Content-Length (chunked transfer and so on) is counted as it is read
- Reading
c.req.bodyas a stream also gets 413 over 1 MiB - Even if the handler swallows the read error, the response is 413
Raise the limit on a route
Add bodyLimit({ maxSize }) only to the routes that take large bodies. The route's limit replaces the default 1 MiB (they do not stack).
import { } from "@senrijs/senri";
.("/import", ({ : 50 * 1024 * 1024 })); // up to 50 MiB on this route
.("/import", async (, ) => {
const = await ..();
return { : .length };
});- For a table route, write
endpoint({ use: [bodyLimit({ maxSize })] }, handler)(Per-route middleware) uploadLimit()andmultipart()also replace the default with their own limits
Change the limit for the whole app
const = ({ : 10 * 1024 * 1024 }); // Infinity removes itA body without Content-Length is wrapped in a stream to be counted, so reading it is slightly slower (tens of µs for a small body). A body with Content-Length is not wrapped, so its speed does not change.
The server-wide limit
serve(app, { maxRequestBodySize }) limits the bodies the server takes. Over it, the response is 413. Without it, there is no limit on any runtime (serve() options).