Skip to content

Commit 7a747ea

Browse files
Merge pull request #2644 from krema/master
docs(raw-body): registering a different parser dynamically
2 parents bc86b81 + 88136dd commit 7a747ea

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

content/faq/raw-body.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ One of the most common use-case for having access to the raw request body is per
99
First enable the option when creating your Nest Express application:
1010

1111
```typescript
12-
const app = await NestFactory.create(AppModule, {
12+
import { NestFactory } from '@nestjs/core';
13+
import type { NestExpressApplication } from '@nestjs/platform-express';
14+
import { AppModule } from './app.module';
15+
16+
// in the "bootstrap" function
17+
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
1318
rawBody: true,
1419
});
1520
await app.listen(3000);
@@ -30,11 +35,26 @@ class CatsController {
3035
}
3136
```
3237

38+
#### Registering a different parser
39+
40+
By default, only `json` and `urlencoded` parsers are registered. If you want to register a different parser on the fly, you will need to do so explicitly.
41+
42+
For example, to register a `text` parser, you can use the following code:
43+
44+
```typescript
45+
app.useBodyParser('text')
46+
```
47+
3348
#### Use with Fastify
3449

3550
First enable the option when creating your Nest Fastify application:
3651

3752
```typescript
53+
import { NestFactory } from '@nestjs/core';
54+
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
55+
import { AppModule } from './app.module';
56+
57+
// in the "bootstrap" function
3858
const app = await NestFactory.create<NestFastifyApplication>(
3959
AppModule,
4060
new FastifyAdapter(),
@@ -59,3 +79,13 @@ class CatsController {
5979
}
6080
}
6181
```
82+
83+
#### Registering a different parser
84+
85+
By default, only `application/json` and `application/x-www-form-urlencoded` parsers are registered. If you want to register a different parser on the fly, you will need to do so explicitly.
86+
87+
For example, to register a `text/plain` parser, you can use the following code:
88+
89+
```typescript
90+
app.useBodyParser('text/plain')
91+
```

0 commit comments

Comments
 (0)