Hello, I'm trying to get something from a message, and I need the rawBody property from the incoming request. How can i get it?
I tried using express.bodyParser () and in my message handler, I was looking for req.rawBody and it was undefined.
I even tried it with connect.bodyParser (), but I still had no luck. I get undefined for rawBody.
I read on the stackoverflow website saying that they removed the rawBody functionality but mentioned that it is a quick solution to add it to our own middleware file. I am a beginner, so I do not know how to do this. The following is a snippet of code.
/** * Module dependencies. */ var express = require('express') , connect = require('connect') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path'); var app = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); //app.use(express.bodyParser()); app.use(connect.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development only if ('development' == app.get('env')) { app.use(express.errorHandler()); } app.get('/', routes.index); app.get('/users', user.list); /**custom stuff**/ app.post('/upload',function(req, res){ console.log(req.header('Content-Type')); console.log(req.header('Host')); console.log(req.header('User-Agent')); console.log(req.rawBody); console.log(req.body); res.send("<h1> Hello the response is "+req.body.username); }); /** end**/ http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); });
Any help with this is much appreciated.
Thanks.
macha source share