I need to get the raw post data for a specific endpoint in a node/express app. I do:
app.use('/paypal/ipn',function(req, res, next) {
var postData='';
req.on('data', function(chunk) {
postData += chunk;
});
req.on('end', function() {
req.rawBody = postData;
console.log('ended buffering. result: ' + req.rawBody);
next();
});
});
What happens is that I get the console.log output in the console and then nothing happens. After a minute or so I see the server returns 200 - probably a timeout. It's like the next() command never executes, or executes and stales.
When I comment out everything, and simply call next():
app.use('/paypal/ipn',function(req, res, next) {
/*
var postData='';
req.on('data', function(chunk) {
postData += chunk;
});
req.on('end', function() {
req.rawBody = postData;
console.log('ended buffering. result: ' + req.rawBody);
next();
});
*/
next();
});
Everything works, that is the endpoint is called (of course the request doesn't contain the rawBody).
So it seems like I'm doing something wrong the way I buffer the rawBody? Something that causes next() not to work?
Call next() without waiting for 'end', to prevent bodyParser and your code from interfering.
app.use(function(req, res, next) {
req.rawBody = '';
req.on('data', function(chunk) {
req.rawBody += chunk;
});
next();
});
app.use(express.bodyParser());
Kudos to https://stackoverflow.com/a/21222540/1891397
I had similar problem, checkout this code (rawBody):
/*
for some very strange reason, while a IE8/IE9 use a XDomain for CORS requests, express.js bodyParser fail to parse
*/
app.use(function(req, res, next) {
if(req.method !== 'POST') {
next();
return;
}
var data = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.rawBody = data;
req.failSafeBody = queryParser(data);
next();
});
});
//se above
//app.use(express.bodyParser());
app.use(function(req, res, next){
if(req.method === 'POST' && (!req.body || Object.keys(req.body).length<1) ) {
req.body = req.failSafeBody;
}
next();
});
app.use(express.methodOverride());
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