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
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.
4
+
5
+
#### Use with Express
6
+
7
+
First enable the option when creating your Nest Express application:
8
+
9
+
```typescript
10
+
const app =awaitNestFactory.create(AppModule, {
11
+
rawBody: true,
12
+
});
13
+
awaitapp.listen(3000);
14
+
```
15
+
16
+
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:
17
+
18
+
```typescript
19
+
import { Controller, Post, RawBodyRequest, Req } from'@nestjs/common';
20
+
import { Request } from'express';
21
+
22
+
@Controller('cats')
23
+
classCatsController {
24
+
@Post()
25
+
create(@Req() req:RawBodyRequest<Request>) {
26
+
const raw =req.rawBody; // returns a `Buffer`.
27
+
}
28
+
}
29
+
```
30
+
31
+
#### Use with Fastify
32
+
33
+
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:
47
+
48
+
```typescript
49
+
import { Controller, Post, RawBodyRequest, Req } from'@nestjs/common';
0 commit comments