Thursday, 27 April 2017

Node JS body-parser get rawBody and body

I'm currently implementing the body parser in a way that is mentioned below

 var rawBodySaver = function (req, res, buf, encoding) {
        if (buf && buf.length) {
            req.rawBody = buf.toString(encoding || 'utf8');
        }
    }
    app.use(bodyParser.urlencoded({ extended: true}))
    app.use(bodyParser.json({ verify: rawBodySaver }));
    app.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));
    app.use(bodyParser.raw({ verify: rawBodySaver, type: function () { return true } }));

Previously, it was just app.use(bodyParser.urlencoded({ extended: true})) and I got my data in req.body.

But due to some new requirements I had to use rawBody. Is there any way where I can get data in their respective formats in both rawBody and body

Right now, only either of rawBody or body works. Both don not work together.



via Abhilash

No comments:

Post a Comment