How to retrieve POST data in NodeJS that was passed using CURL?

327
July 21, 2017, at 08:00 AM

I am trying to pass some parameters to my localhost (which is using nodejs) with a CURL command, but my localhost isn't reading them correctly.

I am doing my POST request to my localhost like this:

curl --data "db_name=auto&old_db=Lab.tar.gz&new_db=627999E00_10.tgz" 
     --noproxy localhost 
     -H "Accept: text/plain" 
     -H "Content-Type: text/plain" 
     -X POST http://localhost:8084/auto

And I try to retrieve my data params with node like this:

app.post('/auto',function(req,res){
    var db_name=req.body.db_name; //undefined
    var old_db=req.body.old_db; //undefined
    var new_db=req.body.new_db; //undefined
    ...
});

But db_name,old_db,new_db are all always undefined.

Also, req.body is an empty object {}

And req.url is just /auto

How to I retrieve the parameters that I passed with my curl program in node?

================== Versions

  • NodeJS v6.5.0
  • ExpressJS v3.14.0
  • CURL v7.22.0

================== Updates

My ExpressJs configuration is the following:

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.urlencoded());
app.use(app.router);
app.use(express.static(_path.join(__dirname, '..', 'Client')));

Also I tried some other curl variations:

curl --noproxy localhost 
     -H "Accept: text/plain" 
     -H "Content-Type: application/json" 
     -X POST 
     -d "{'db_name':'auto','old_db':'Lab.tar.gz','new_db':'627999E00_10.tgz'}" 
     http://localhost:8084/auto
Answer 1

Finally got it to work with the help from @J.Chen and @hanshenrik !

If I change the format of the --data in my CURL request,

AND if I remove the -H "Accept: text/plain" -H "Content-Type: application/json" from the CURL request,

Then my nodeJS Application finally sees the parameters in req.body.

So my final code is:

curl --noproxy localhost 
     -X POST 
     -d "{'db_name':'auto','old_db':'Lab.tar.gz','new_db':'627999E00_10.tgz'}" 
     http://localhost:8084/auto

And my NodeJS code is:

app.post('/auto',function(req,res){
    var parseMe = Object.keys(req.body)[0];
    var parsedParams = JSON.parse(parseMe);
    var db_name=parsedParams.db_name; 
    var old_db=parsedParams.old_db; 
    var new_db=parsedParams.new_db; 
    ...
});
Answer 2

Try adding the json body parser middleware

const bodyParser = require('body-parser');
app.use(bodyParser.json());
READ ALSO
Display js-object info somehow in html

Display js-object info somehow in html

So lets say I had an object in js that looked a little like this

301
Regex to find pattern matching two strings with 'x' number of spaces between them

Regex to find pattern matching two strings with 'x' number of spaces between them

I have a situation where I am looking for specific words following one another, but I don't know how many spaces there are between those words, there are also parenthesis in there now that I think of it

245
How to import a whole different HTML file into an iframe?

How to import a whole different HTML file into an iframe?

I have an HTML doc that I want to be loaded into my iframe and then the print preview to pop up on google chromeThis is what I have done so far but it doesn't seem to work:

300
push property value to object, store and accumulate, then randomly select 2

push property value to object, store and accumulate, then randomly select 2

Ok, can't get thisI'm trying to push an object property key:value' to an empty object, store it in a variable, then next call retrieve that updated object, and add a new key:value pair to it, then finally grab 2 different random values from the created...

186