Skip to content

Commit 9f7da88

Browse files
Merge pull request #2325 from tolgap/docs/raw-body
docs(faq): add raw body option usage to faq
2 parents 847b0fb + 673e258 commit 9f7da88

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

content/faq/raw-body.md

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

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)