Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressjs raw body

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); 
like image 905
Andrey Kon Avatar asked Mar 29 '12 06:03

Andrey Kon


People also ask

How do I get raw on Body Express?

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.

What does Express raw do?

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.

Is bodyParser deprecated?

'bodyParser' is deprecated. // If you are using Express 4.16+ you don't have to import body-parser anymore.

Does Express need body parser?

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.


1 Answers

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); 
like image 183
stewe Avatar answered Oct 17 '22 01:10

stewe