Skip to content

Commit 579752e

Browse files
committed
docs(faq): add raw body option usage
1 parent 1ab9513 commit 579752e

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

content/faq/raw-body.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### Raw body
2+
3+
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 = await NestFactory.create(AppModule, {
13+
rawBody: true,
14+
});
15+
await app.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+
class CatsController {
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:
36+
37+
```typescript
38+
const app = await NestFactory.create<NestFastifyApplication>(
39+
AppModule,
40+
new FastifyAdapter()
41+
{
42+
rawBody: true,
43+
}
44+
);
45+
await app.listen(3000);
46+
```
47+
48+
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';
52+
import { FastifyRequest } from 'fastify';
53+
54+
@Controller('cats')
55+
class CatsController {
56+
@Post()
57+
create(@Req() req: RawBodyRequest<FastifyRequest>) {
58+
const raw = req.rawBody; // returns a `Buffer`.
59+
}
60+
}
61+
```

src/app/homepage/menu/menu.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ export class MenuComponent implements OnInit {
248248
{ title: 'Serverless', path: '/faq/serverless' },
249249
{ title: 'HTTP adapter', path: '/faq/http-adapter' },
250250
{ title: 'Global path prefix', path: '/faq/global-prefix' },
251+
{ title: 'Raw body', path: '/faq/raw-body' },
251252
{ title: 'Hybrid application', path: '/faq/hybrid-application' },
252253
{ title: 'HTTPS & multiple servers', path: '/faq/multiple-servers' },
253254
{ title: 'Request lifecycle', path: '/faq/request-lifecycle' },

src/app/homepage/pages/faq/faq.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GlobalPrefixComponent } from './global-prefix/global-prefix.component';
77
import { HttpAdapterComponent } from './http-adapter/http-adapter.component';
88
import { HybridApplicationComponent } from './hybrid-application/hybrid-application.component';
99
import { MultipleServersComponent } from './multiple-servers/multiple-servers.component';
10+
import { RawBodyComponent } from './raw-body/raw-body.component';
1011
import { RequestLifecycleComponent } from './request-lifecycle/request-lifecycle.component';
1112
import { ServerlessComponent } from './serverless/serverless.component';
1213

@@ -31,6 +32,11 @@ const routes: Routes = [
3132
component: HttpAdapterComponent,
3233
data: { title: 'HTTP adapter - FAQ' },
3334
},
35+
{
36+
path: 'raw-body',
37+
component: RawBodyComponent,
38+
data: { title: 'Raw Body' },
39+
},
3440
{
3541
path: 'request-lifecycle',
3642
component: RequestLifecycleComponent,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { BasePageComponent } from '../../page/page.component';
3+
4+
@Component({
5+
selector: 'app-raw-body',
6+
templateUrl: './raw-body.component.html',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
8+
})
9+
export class RawBodyComponent extends BasePageComponent {}

0 commit comments

Comments
 (0)