Ruslan Alekseyev
Ruslan Alekseyev

Reputation: 63

Nest.js raw bodyParser doesn't fire json function

I need to get raw body request for generating signature. and when i found solution i faced with issue that json function not working example below :

import { Injectable, NestMiddleware } from '@nestjs/common';
import { json } from 'body-parser';
import { IncomingMessage } from 'http';

@Injectable()
export class RawBodyMiddlewareMiddleware implements NestMiddleware {
  public use(req: IncomingMessage, res, next: () => any): any {
    json({ //json function not fired
      verify: (req, res, buffer) => {
        console.log(buffer);
        if (Buffer.isBuffer(buffer)) {
          const rawBody = Buffer.from(buffer);
          req['parsedRawBody'] = rawBody;
        }
        return true;
      },
    })(req, res as any, next);
  }
}

Upvotes: 0

Views: 6895

Answers (2)

Sairam Vemula
Sairam Vemula

Reputation: 36

Just set rawBody to true and you can access it in req.rawBody

const app = await NestFactory.create(AppModule, {
rawBody: true,
});

Upvotes: 1

Ruslan Alekseyev
Ruslan Alekseyev

Reputation: 63

After investigation i understand that for some reason i need to mark bodyParser as false.

const app = await NestFactory.create(AppModule, {
    bodyParser: false,
  });

Upvotes: 1

Related Questions