How can I access raw body of request object given to me by expressjs?
var express = require('./node_modules/express'); var app = express.createServer(); app.post('/', function(req, res) { console.log(req.body); //says 'undefined' }); app.listen(80);
Inject the raw body buffer into the webhook route rawBody = buf. toString(encoding || "utf8"); } }; This will inject the buffer bytes (with the proper encoding) into the attribute rawBody of the express. Request object received by your server.
express.raw([options]) This is a built-in middleware function in Express. It parses incoming request payloads into a Buffer and is based on body-parser. Returns middleware that parses all bodies as a Buffer and only looks at requests where the Content-Type header matches the type option.
'bodyParser' is deprecated. // If you are using Express 4.16+ you don't have to import body-parser anymore.
Keep it simple : if you used post request so you will need the body of the request, so you will need body-parser . No need to install body-parser with express , but you have to use it if you will receive post request.
Something like this should work:
var express = require('./node_modules/express'); var app = express.createServer(); app.use (function(req, res, next) { var data=''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.body = data; next(); }); }); app.post('/', function(req, res) { console.log(req.body); }); app.listen(80);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With