Body parsing
Defaults
Section titled “Defaults”JSON (application/json and +json types) and urlencoded bodies are parsed automatically, with a 100kb limit, exactly like @nestjs/platform-express. Requests without a body, or with an unrecognized Content-Type, leave req.body undefined.
- Invalid JSON →
400 Bad Requestwith the parser message. - Top-level primitives (
"str",42) are rejected in strict mode, like body-parser. - Bodies over the limit →
413 Payload Too Large. - Unsupported charsets →
415.
Raise the limit for the whole app:
new BunAdapter({ bodyLimit: '5mb' });Raw body
Section titled “Raw body”Webhook signatures need the exact bytes:
const app = await NestFactory.create<NestBunApplication>(AppModule, new BunAdapter(), { rawBody: true });@Post('stripe')webhook(@RawBody() raw: Buffer, @Headers('stripe-signature') sig: string) { ... }More parsers
Section titled “More parsers”app.useBodyParser('text'); // text/plain → stringapp.useBodyParser('raw', { type: 'image/*' }); // → Bufferapp.useBodyParser('json', { limit: '10mb', type: 'application/vnd.custom+json' });Parsers run in registration order and each one only handles requests whose Content-Type matches its type.
Turning parsing off
Section titled “Turning parsing off”NestFactory.create(AppModule, new BunAdapter(), { bodyParser: false });Then read the body yourself from the Web Request:
@Post('upload')async upload(@Req() req: BunRequest) { const form = await req.native.formData(); const file = form.get('file') as File;}req.native is the underlying Web Request. Its json(), text(), arrayBuffer() and formData() are all available as long as no parser consumed the body first.