Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

middleware for saving raw post data in the request object won't "next" and cause timeout

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?

like image 711
shaharsol Avatar asked Feb 13 '23 17:02

shaharsol


2 Answers

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

like image 84
Oren Solomianik Avatar answered Feb 16 '23 10:02

Oren Solomianik


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());
like image 33
h0x91B Avatar answered Feb 16 '23 10:02

h0x91B