xShirase
xShirase

Reputation: 12399

Node.JS POST request to Express app secured by passport

Mechanism :

I am making this POST request to my API :

request.post('http://localhost:9876/api/alerts',{body: "test"}, function (err, res, body) {
        if (err) {
            self.emit('log','ERROR : API error - '+err);
        }
        self.emit('log','RESPONSE - '+res.statusCode);
    });

On the server side, I have :

app.post('/api/alerts',function(req,res){
    console.log(req);
    res.status(200).send('OK');
});

Communication is made and it returns a 200 status. But on the server side, I see no trace of my request's body.

The full 'req' log is available here : https://gist.github.com/xShirase/0f9de0048e5cfa40a98c , the most relevant part being :

body: {},

I was wondering if it was coming from the Passport middleware that I use to secure the rest of my routes, or if I just botched the client request...

I have tried many different requests formats on the client side, and nothing has worked, and I have very little experience with Passport, so please let me know where my problem comes from.

Upvotes: 0

Views: 563

Answers (1)

mscdex
mscdex

Reputation: 106696

Unless you have a (custom) middleware earlier up in the route/middleware chain that is doing something like:

app.use(function(req, res, next) {
  var buffer = '';
  req.setEncoding('utf8');
  req.on('data', function(d) {
    buffer += d;
  }).on('end', function() {
    req.body = buffer;
    next();
  });
});

then you probably shouldn't expect req.body to be populated since the common body parsing modules expect a Content-Type of one of application/json, application/x-www-form-urlencoded, or multipart/form-data. Your request() doesn't seem to be setting any of these, which really is correct since it's just free-form data, but that means no middleware is reading request data.

Upvotes: 1

Related Questions