Expressjs Body Raw

How can I access the raw body of the request object provided 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); 
+43
express
Mar 29 2018-12-12T00:
source share
10 answers

By default, express does not buffer data unless you add middleware for this. A simple solution is to follow the example in @Stewe's answer below, which will simply concatenate all the data yourself. eg.

 var concat = require('concat-stream'); app.use(function(req, res, next){ req.pipe(concat(function(data){ req.body = data; next(); })); }); 

The disadvantage of this is that you have now moved all the contents of the POST body to RAM as a continuous fragment, which may not be needed. Another option that is worth considering, but depends on how much data you need to process in the body of the message, will process the data as a stream instead.

For example, with XML, you can use an XML parser that supports XML parsing because it comes in fragments. One such parser will be an XML stream . You are doing something like this:

 var XmlStream = require('xml-stream'); app.post('/', function(req, res) { req.setEncoding('utf8'); var xml = new XmlStream(req); xml.on('updateElement: sometag', function(element) { // DO some processing on the tag }); xml.on('end', function() { res.end(); }); }); 
+16
Mar 29 '12 at 18:43
source share

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); 
+51
Mar 29 '12 at 7:15
source share

Using middleware bodyParser.text() will put the text body in req.body .

 app.use(bodyParser.text({type: '*/*'})); 

If you want to limit the processing of the text body to specific routes or to place content types, you can do this too.

 app.use('/routes/to/save/text/body/*', bodyParser.text({type: 'text/plain'})); //this type is actually the default app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); 

If you need a raw Buffer , you can use bodyParse.raw() .

 app.use(bodyParser.raw({type: '*/*'})); 

Note: this answer was tested on node v0.12.7, express 4.13.2 and body parser 1.13.3.

+32
Aug 12 '15 at 10:03
source share

Place the following middleware in front of the bodyParser middleware. It will collect raw body data in request.rawBody and will not interfere with bodyParser.

 app.use(function(req, res, next) { var data = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); }); app.use(express.bodyParser()); 
+26
Nov 26 '12 at 13:25
source share

So, it seems that Express bodyParser parses only incoming data if the content-type parameter is set to one of the following values:

  • application/x-www-form-urlencoded
  • application/json
  • multipart/form-data

In all other cases, he does not even bother to read the data.

You can change the line number. 92 of express / node_modules / connect / lib / middleware / bodyParser.js from

 } else { next(); } 

To:

 } else { var data=''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); } 

And then, read req.rawBody from your code.

+7
Mar 29 '12 at 17:52
source share
 app.use(bodyParser.json({ verify: function (req, res, buf, encoding) { req.rawBody = buf; } })); app.use(bodyParser.urlencoded({ extended: false, verify: function (req, res, buf, encoding) { req.rawBody = buf; } })); 
+6
Nov 30 '15 at 2:32
source share

If you have problems with the solutions described above that interfere with regular mail requests, something like this might help:

 app.use (function(req, res, next) { req.rawBody = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { req.rawBody += chunk }); }); 

Additional information and source: https://github.com/visionmedia/express/issues/897#issuecomment-3314823

+5
Dec 22
source share

BE CAREFUL with these other answers as they will not play correctly with bodyParser if you want to support json, urlencoded etc. too. To make it work with bodyParser, you must force the handler to register only on the Content-Type header (s) you care about, just like bodyParser itself.

To get the contents of the original request content with Content-Type: "text/xml" in req.rawBody , you can do:

 app.use(function(req, res, next) { var contentType = req.headers['content-type'] || '' , mime = contentType.split(';')[0]; if (mime != 'text/xml') { return next(); } var data = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { req.rawBody = data; next(); }); }); 
+3
Apr 04 '14 at 1:07 on
source share

If you want the body to be in the form of a buffer:

 var rawParser = function(req, res, next) { var chunks = []; req.on('data', function(chunk) { chunks.push(chunk) }); req.on('end', function() { req.body = Buffer.concat(chunks); next(); }); } 

or

 var rawParser = bodyParser.raw({type: '*/*'}); 

and then:

 app.put('/:name', rawParser, function(req, res) { console.log('isBuffer:', Buffer.isBuffer(req.body)); }) 

or for all routes:

 app.use(bodyParser.raw({type: '*/*'})); 
+3
May 28 '16 at 3:54
source share

It seems now has become much easier !

The body-parser module can now analyze raw and text data, which makes the task a single line :

 app.use(bodyParser.text({type: 'text/plain'})) 

OR

 app.use(bodyParser.raw({type: 'application/binary'})) 

Both lines just populate the body property, so get the text using res.body . bodyParser.text() will give you a UTF8 string, and bodyParser.raw() will give you raw data.

This is the complete code for text / simple data:

 var express = require('express') var bodyParser = require('body-parser') var app = express() app.use(bodyParser.text({type: 'text/plain'})) app.post('/', function (req, res, next) { console.log('body:\n' + req.body) res.json({msg: 'success!'}) next() }) 

See here for full documentation: https://www.npmjs.com/package/body-parser

I used Express 4.16 and Body-Parser 1.18

+3
Mar 07 '18 at 10:55
source share



All Articles