#rawBody is not recognised by nestjs but its there in documentation.

1 messages · Page 1 of 1 (latest)

serene eagle
#
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';
vale latch
#

What is your version of @nestjs/common?

serene eagle
# vale latch 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"
}
glacial imp
vale latch
#

Yeah, RawBodyRequest was a dded in 9.1.6

serene eagle
serene eagle
vale latch
#

Actually, hang on a moment

glacial imp
serene eagle
#
/**
 * 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);
}
vale latch
vale latch
#

Yep

#

Weird that I see it under v8 docs though

serene eagle
#

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?

vale latch
#

Can you run nest info for me really quickly? Or actually, what;s your package manager (npm, yarn, or pnpm)

serene eagle
#

wait a min

#

i am using npm

glacial imp
#

i remember use body parser in v7

vale latch
#

Are you on unix or Windows?

glacial imp
#

`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);
}`

serene eagle
#

ubuntu 20.04

vale latch
#

Okay, run this for me: npm ls --depth=0 | grep @nestjs/common

serene eagle
# vale latch 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]

vale latch
#

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

serene eagle
glacial imp
vale latch
#

@nestjs/common and @nestjs/core. Honestly, keeping the @nestjs/* deps together is usually a good idea

serene eagle
vale latch
#

But core, common, and platform-* all have to be the same version

serene eagle
vale latch
#

npm upgrade @nestjs/common @nestjs/core @nestjs/platform-express

#

That should respect semver

serene eagle
#

ok let me try

serene eagle
#

for plain and xml body it is coming empty

vale latch
#

Got a minimum reproduction?

serene eagle
#

can't get you

vale latch
#

A sample repository that shows the problem you're having

serene eagle
#

ok give me some time, let me check this in fresh nestjs project.

serene eagle
#

still same issue in v9 also

serene eagle
#

@vale latch any update?

vale latch
#

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>
vale latch
#

You didn't have an xml parser installed

#

So even if you added an xml parser, you'd still need to add a way to set the rawBody yourself

serene eagle
#

ok nest only provided for json's?

#

If it like that then better i write my own middleware.

vale latch
#

Yep

serene eagle
#
  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.

serene eagle
# vale latch Yep

i guess nestjs is not parsing text body too? only json and urlencoded out of the box.

serene eagle
#

@vale latch @glacial imp thanks for the support!

slate jacinth
#

Any updates on this? I get only an empty response when using the rawBody

vale latch
slate jacinth
#

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,
    })
  );
vale latch
#

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