I use the http module and I need to get the req.body currently I try with the following without success .
http.createServer(function (req, res) {
console.log(req.body);
this return undfiend ,any idea why? I send via postman some short text...
I use the http module and I need to get the req.body currently I try with the following without success .
http.createServer(function (req, res) {
console.log(req.body);
this return undfiend ,any idea why? I send via postman some short text...
Here's a very simple without any framework (Not express way).
var http = require('http');
var querystring = require('querystring');
function processPost(request, response, callback) {
var queryData = "";
if(typeof callback !== 'function') return null;
if(request.method == 'POST') {
request.on('data', function(data) {
queryData += data;
if(queryData.length > 1e6) {
queryData = "";
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
request.connection.destroy();
}
});
request.on('end', function() {
request.post = querystring.parse(queryData);
callback();
});
} else {
response.writeHead(405, {'Content-Type': 'text/plain'});
response.end();
}
}
Usage example:
http.createServer(function(request, response) {
if(request.method == 'POST') {
processPost(request, response, function() {
console.log(request.post);
// Use request.post here
response.writeHead(200, "OK", {'Content-Type': 'text/plain'});
response.end();
});
} else {
response.writeHead(200, "OK", {'Content-Type': 'text/plain'});
response.end();
}
}).listen(8000);
In Postman of the 3 options available for content type select "X-www-form-urlencoded".
app.use(bodyParser.urlencoded())
With:
app.use(bodyParser.urlencoded({
extended: true
}));
See https://github.com/expressjs/body-parser
The 'body-parser' middleware only handles JSON and urlencoded data, not multipart
req.body
is a Express feature, as far as I know... You can retrieve the request body like this with the HTTP module: