This document explains how NestJS handles HTTP request body parsing across different platform adapters (Express and Fastify). It covers default parser registration, raw body access for webhook signature verification, and the configuration of custom content types and limits.
NestJS provides automatic body parsing for incoming HTTP requests through platform-specific adapters. The framework abstracts the underlying parser implementations (Express body-parser and Fastify addContentTypeParser) into a consistent API.
Key capabilities include:
rawBody option, enabling access to the original Buffer for signature verification.The following diagram illustrates how application options propagate through the NestApplication to the specific platform adapters.
Body Parser Registration Flow
Sources: packages/core/nest-application.ts178-205 packages/platform-express/adapters/express-adapter.ts306-334 packages/platform-fastify/adapters/fastify-adapter.ts619-670
Body parsers are automatically registered during the init() phase of the application lifecycle. The NestApplication checks the bodyParser flag in appOptions before calling the adapter's registration method.
| Step | Function/Method | Description |
|---|---|---|
| 1 | NestApplication.init() | Triggers the initialization sequence. packages/core/nest-application.ts178-189 |
| 2 | registerParserMiddleware() | Determines if rawBody is required and calls the adapter. packages/core/nest-application.ts201-205 |
| 3 | adapter.registerParserMiddleware() | Platform-specific logic to apply middleware. packages/core/adapters/http-adapter.ts180 |
Sources: packages/core/nest-application.ts178-205 packages/core/adapters/http-adapter.ts180
The ExpressAdapter utilizes standard Express middleware. It uses a utility getBodyParserOptions to inject a verify callback if rawBody is enabled.
express.json(). packages/platform-express/adapters/express-adapter.ts308-311express.urlencoded({ extended: true }). packages/platform-express/adapters/express-adapter.ts312-315Sources: packages/platform-express/adapters/express-adapter.ts306-319 packages/platform-express/adapters/utils/get-body-parser-options.util.ts
The FastifyAdapter uses Fastify's native addContentTypeParser API. Unlike Express, Fastify requires parsers to be registered before the server starts listening.
rawBody if requested. packages/platform-fastify/adapters/fastify-adapter.ts759-775querystringParse from fast-querystring. packages/platform-fastify/adapters/fastify-adapter.ts777-793_isParserRegistered flag to ensure parsers are only registered once. packages/platform-fastify/adapters/fastify-adapter.ts619-633Sources: packages/platform-fastify/adapters/fastify-adapter.ts619-633 packages/platform-fastify/adapters/fastify-adapter.ts759-793
For use cases like Stripe or GitHub webhooks, the raw request body (unmodified bytes) is required to verify cryptographic signatures. When rawBody: true is set in the application options, NestJS ensures the raw buffer is preserved.
Implementation Detail: When enabled, the rawBody property is attached to the request object. In Fastify, this happens within the content type parser: (req.raw as any).rawBody = body;. packages/platform-fastify/adapters/fastify-adapter.ts655
The @nestjs/common package provides the RawBodyRequest<T> type to allow type-safe access to the rawBody property.
Entity Mapping: Request to Code
Sources: packages/platform-fastify/adapters/fastify-adapter.ts6-12 packages/platform-fastify/adapters/fastify-adapter.ts650-656
The ExpressAdapter exposes useBodyParser() which wraps the standard body-parser types: json, urlencoded, raw, and text.
API Signature: packages/platform-express/adapters/express-adapter.ts321-334
Sources: packages/platform-express/interfaces/nest-express-application.interface.ts108-111
Fastify allows registering parsers for specific MIME types or using Regular Expressions.
API Signature: packages/platform-fastify/adapters/fastify-adapter.ts635-670
Sources: packages/platform-fastify/interfaces/nest-fastify-application.interface.ts66-70
If you need to handle body parsing entirely manually (e.g., using a specific middleware that Nest doesn't wrap), you can disable the default behavior:
Logic: Setting bodyParser to false prevents registerParserMiddleware() from executing in the init() method. packages/core/nest-application.ts187-188
| Option | Type | Default | Description |
|---|---|---|---|
bodyParser | boolean | true | Whether to register default parsers. |
rawBody | boolean | false | Whether to preserve the raw request buffer. |
Sources: packages/core/nest-application.ts186-189 packages/common/interfaces/nest-application-options.interface.ts
The AbstractHttpAdapter defines the contract for body parsing, which is then implemented by platform-specific classes.
Sources: packages/core/adapters/http-adapter.ts8-193 packages/common/interfaces/http/http-server.interface.ts17-102
Refresh this wiki