TS2305: Module '"@nestjs/common"' has no exported member 'RawBodyRequest'.
8 | Req,
9 | Response,
> 10 | RawBodyRequest
| ^^^^^^^^^^^^^^
11 | } from '@nestjs/common';
12 | import {Response as Res, Request} from 'express';
13 | import { DeploymentsGateway } from './deployments.gateway';
ERROR in apps/simulator/src/main.ts:13:53
TS2769: No overload matches this call.
Overload 1 of 2, '(module: any, options?: NestApplicationOptions): Promise<INestApplication>', gave the following error.
Argument of type '{ rawBody: boolean; }' is not assignable to parameter of type 'NestApplicationOptions'.
Object literal may only specify known properties, and 'rawBody' does not exist in type 'NestApplicationOptions'.
Overload 2 of 2, '(module: any, httpAdapter: AbstractHttpAdapter<any, any, any>, options?: NestApplicationOptions): Promise<INestApplication>', gave the following error.
Argument of type '{ rawBody: boolean; }' is not assignable to parameter of type 'AbstractHttpAdapter<any, any, any>'.
Object literal may only specify known properties, and 'rawBody' does not exist in type 'AbstractHttpAdapter<any, any, any>'.
11 |
12 | async function bootstrap() {
> 13 | const app = await NestFactory.create(AppModule, { rawBody: true });
| ^^^^^^^^^^^^^
14 | app.use(helmet());
15 | app.enableCors();
16 | const globalPrefix = 'api';
#rawBody is not recognised by nestjs but its there in documentation.
1 messages · Page 1 of 1 (latest)
What is your version of @nestjs/common?
"dependencies": {
"@nestjs-modules/mailer": "^1.6.0",
"@nestjs/common": "^8.1.1",
"@nestjs/config": "^1.0.2",
"@nestjs/core": "^8.1.1",
"@nestjs/jwt": "^8.0.0",
"@nestjs/mongoose": "^8.0.1",
"@nestjs/platform-express": "^8.1.1",
"@nestjs/platform-socket.io": "^8.1.1",
"@nestjs/throttler": "^2.0.0",
"@nestjs/websockets": "^8.1.1"
}
following this doc: https://docs.nestjs.com/faq/raw-body
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
should follow this https://docs.nestjs.com/v8/faq/raw-body
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Yeah, RawBodyRequest was a dded in 9.1.6
i followed the same
so, i need to upgrade?
Actually, hang on a moment
can send the snippet for both main.ts and the controller?
main.ts
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
*/
import { Logger, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
import * as helmet from 'helmet';
async function bootstrap() {
const app = await NestFactory.create(AppModule, { rawBody: true });
app.use(helmet());
app.enableCors();
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
app.useGlobalPipes(
new ValidationPipe({
disableErrorMessages: true,
}),
)
const port = process.env.PORT || 3333;
await app.listen(port,'0.0.0.0', () => {
Logger.log('Listening at http://0.0.0.0:' + port + '/' + globalPrefix);
});
}
bootstrap();
controller.ts
import {
All,
Body,
Controller,
Headers,
HttpException,
HttpStatus,
Req,
Response,
RawBodyRequest
} from '@nestjs/common';
import {Response as Res, Request} from 'express';
import { DeploymentsGateway } from './deployments.gateway';
import { ServeDeploymentService } from './serve-deployment.service';
@Controller('serveDeployment')
export class ServeDeploymentController {
constructor(
private websocketGateway: DeploymentsGateway,
private serveDeploymentService: ServeDeploymentService
) {}
@All('*')
async executeApi(
@Req() req : Request,
@Req() req1: RawBodyRequest<Request>,
@Response() res : Res,
@Body() body: any,
@Headers() headers
) {
console.log(req1.rqwBody);
}
Ah! Okay, here it is. 9.0.5: https://github.com/nestjs/nest/pull/9926
so this v9 feature?
yes me too
i guess either i need to upgrade to v9 or i need to check an alternate solution for this
Nestjs have any alternate for this in v8?
Can you run nest info for me really quickly? Or actually, what;s your package manager (npm, yarn, or pnpm)
i remember use body parser in v7
Are you on unix or Windows?
`import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger, ValidationPipe } from '@nestjs/common';
import * as bodyParser from 'body-parser';
const logger = new Logger('Main');
const port = process.env.PORT || 3000;
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
bodyParser: false,
});
app.useGlobalPipes(new ValidationPipe({ transform: true }));
/**
- Extract raw body Buffer to verify Webhook Integrity
*/
const rawBodyBuffer = (req, res, buf: Buffer, encoding) => {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
};
app.use(
bodyParser.urlencoded({
verify: rawBodyBuffer,
extended: true
})
);
app.use(
bodyParser.json({
verify: rawBodyBuffer
})
);
await app.listen(port);
}`
ubuntu 20.04
Okay, run this for me: npm ls --depth=0 | grep @nestjs/common
├── UNMET PEER DEPENDENCY @nestjs/[email protected]
npm ERR! peer dep missing: @nestjs/common@^7.0.9, required by @nestjs-modules/[email protected]
npm ERR! peer dep missing: @nestjs/common@^7.0.0, required by @nestjs/[email protected]
npm ERR! peer dep missing: @nestjs/core@^7.0.9, required by @nestjs-modules/[email protected]
npm ERR! peer dep missing: @nestjs/core@^7.0.0, required by @nestjs/[email protected]
npm ERR! peer dep missing: mongoose@^5.11.15 || ^5.12.4, required by @nestjs/[email protected]
npm ERR! peer dep missing: @angular/compiler@>= 12.0.0 < 13.0.0, required by @angular-eslint/[email protected]
npm ERR! peer dep missing: @angular/compiler@>= 12.0.0 < 13.0.0, required by @angular-eslint/[email protected]
npm ERR! peer dep missing: @angular/compiler@>= 12.0.0 < 13.0.0, required by @angular-eslint/[email protected]
npm ERR! peer dep missing: @angular/[email protected], required by @angular/[email protected]
npm ERR! peer dep missing: @popperjs/core@^2.10.1, required by [email protected]
npm ERR! peer dep missing: @angular/core@>=9.0.0, required by [email protected]
npm ERR! peer dep missing: @angular/platform-browser-dynamic@>=9.0.0, required by [email protected]
npm ERR! peer dep missing: webpack@^5.30.0, required by @angular-devkit/[email protected]
npm ERR! peer dep missing: webpack@^5.30.0, required by @ngtools/[email protected]
Okay, 8.1.1. It's available in 8.4.5 from what I was seeing in the GitHub repo, so you probably just need to update the minor version and not do a full major upgrade
will it give me raw body data in request request?
only @nestjs/common upgrade?
yes, by accessing the req.rawBody
@nestjs/common and @nestjs/core. Honestly, keeping the @nestjs/* deps together is usually a good idea
let me try!
But core, common, and platform-* all have to be the same version
do you have any command?
npm upgrade @nestjs/common @nestjs/core @nestjs/platform-express
That should respect semver
ok let me try
after upgrade error is not coming but i am not getting raw body from my request. when i send json data in the body then it is coming in req.rawBody
for plain and xml body it is coming empty
Got a minimum reproduction?
can't get you
ok give me some time, let me check this in fresh nestjs project.
clone and check https://github.com/amitagarwal-dev/nest-rawdata.git
still same issue in v9 also
just send a xml body from postman to http://127.0.0.1:3000/
@vale latch any update?
I get the raw buffer as expected
<Buffer 7b 22 6b 65 79 22 3a 22 76 61 6c 75 65 22 2c 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d>
are you sending xml or json?
You didn't have an xml parser installed
Take a look at how the rawBody option works under the hood: https://github.com/nestjs/nest/blob/master/packages/platform-express/adapters/express-adapter.ts#L210
If it's there, nest will add a verify to the JSON and URL encoded parses that sets req.rawBody.
So even if you added an xml parser, you'd still need to add a way to set the rawBody yourself
ok nest only provided for json's?
If it like that then better i write my own middleware.
Yep
your suggested code is actually not working for my use case, so i modified it like this.
const app = await NestFactory.create(AppModule,{
bodyParser: false
});
/**
* Extract raw body Buffer to accept different request body type
*/
const rawBodyBuffer = (req, res, buf: Buffer, encoding) => {
console.log("buff",buf.toString());
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
};
app.use(
bodyParser.json()
);
app.use(
bodyParser.text()
);
app.use(
bodyParser.urlencoded()
);
app.use(
bodyParser.raw({
verify: rawBodyBuffer,
type:["*/*"]
})
);
i just want the raw body of other content types apart from json.
i guess nestjs is not parsing text body too? only json and urlencoded out of the box.
@vale latch @glacial imp thanks for the support!
Any updates on this? I get only an empty response when using the rawBody
Got a reproduction? In amitagarwal's case, the addition of req.rawBody needed to be handled for the xml parser.
I have no other parsers:
const app = await NestFactory.create(AppModule, {
cors: true,
bodyParser: false,
rawBody: true,
});
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
})
);
The rawBody option adds req.rawBody along with parsing the body into the json format at req.body. With bodyParser: false, Nest won't add req.rawBody