Vikas Kumar Vikas Kumar - 1 year ago 128
Node.js Question

Getting POST parameters not working

I'm trying to send a post parameter (key: test, value: somevlaue) using PostMan using restify framework. For this I've used 2 methods and both are not working:

1st one shows this error:

"code": "InternalError",
"message": "Cannot read property 'test' of undefined"

2nd one (commented) shows only Error: someerror

Am I doing something wrong?

Here's my code:

var restify=require('restify');
var fs=require('fs');
var qs = require('querystring');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var controllers = {};

var server=restify.createServer();"/get", function(req, res, next){
res.send({value: req.body.test,
error: "someerror"});

//**********METHOD TWO*********************

if (req.method == 'POST') {
var body = '';

req.on('data', function (data) {
body += data;

req.on('end', function () {
var post = qs.parse(body);
Data: post.test,
Error: "Someerror"

server.listen(8081, function (err) {
if (err)
console.log('App is ready at : ' + 8081);

Answer Source

It looks like you might have your bodyparser set up incorrectly. According to the docs under the body parser section you set up the parser in this manner:

    maxBodySize: 0,
    mapParams: true,
    mapFiles: false,

The default is to map the data to req.params but you can change this and map it to req.body by setting the mapParams option to false


Blocks your chain on reading and parsing the HTTP request body. Switches on Content-Type and does the appropriate logic. application/json, application/x-www-form-urlencoded and multipart/form-data are currently supported.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download