0

I am using express2.js when using req.body I get undefined or empty {}:

exports.post = function (req: express3.Request, res: express3.Response) {
    console.log(req.body);    
});

I have the following configurations:

app.use(express.bodyParser());
app.use(app.router);

app.post('/getuser', routes.getuserprofile.post);

The request body is in XML and I checked the request header which was correct.

Amin
  • 73
  • 1
  • 7

2 Answers2

1

I missed the part where you had XML. I guess req.body doesn't parse by default.

If you are using Express 2.x then perhaps this solution by @DavidKrisch is adequate (copied below)

// This script requires Express 2.4.2
// It echoes the xml body in the request to the response
//
// Run this script like so:
// curl -v -X POST -H 'Content-Type: application/xml' -d '<hello>world</hello>' http://localhost:3000
var express = require('express'),
    app = express.createServer();

express.bodyParser.parse['application/xml'] = function(data) {
    return data;
};

app.configure(function() {
    app.use(express.bodyParser());
});


app.post('/', function(req, res){
    res.contentType('application/xml');
    res.send(req.body, 200);
});

app.listen(3000);
Plato
  • 10,812
  • 2
  • 41
  • 61
0

I don't believe express.bodyParser() supports XML. It only supports url-encoded parameters and JSON.

From: http://expressjs.com/api.html#middleware

bodyParser()

Request body parsing middleware supporting JSON, urlencoded, and multipart requests.

Michael
  • 34,873
  • 17
  • 75
  • 109