You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nest applications have an option to provide the raw contents of the request body. It is disabled by default.
4
+
5
+
One of the most common use-case for having access to the raw request body is performing webhook signature verifications. Usually to perform webhook signature validations the unserialized request body is required to calculate an HMAC hash.
6
+
7
+
#### Use with Express
8
+
9
+
First enable the option when creating your Nest Express application:
10
+
11
+
```typescript
12
+
const app =awaitNestFactory.create(AppModule, {
13
+
rawBody: true,
14
+
});
15
+
awaitapp.listen(3000);
16
+
```
17
+
18
+
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
19
+
20
+
```typescript
21
+
import { Controller, Post, RawBodyRequest, Req } from'@nestjs/common';
22
+
import { Request } from'express';
23
+
24
+
@Controller('cats')
25
+
classCatsController {
26
+
@Post()
27
+
create(@Req() req:RawBodyRequest<Request>) {
28
+
const raw =req.rawBody; // returns a `Buffer`.
29
+
}
30
+
}
31
+
```
32
+
33
+
#### Use with Fastify
34
+
35
+
First enable the option when creating your Nest Fastify application:
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
49
+
50
+
```typescript
51
+
import { Controller, Post, RawBodyRequest, Req } from'@nestjs/common';
0 commit comments