2

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
Katie
  • 45,622
  • 19
  • 93
  • 125
  • Does putting `app.use(express.bodyParser());` in the beginning do anything? – J. Chen Jul 20 '17 at 22:55
  • 1
    @J.Chen I already had that :/ I'll add that info to the question – Katie Jul 20 '17 at 22:56
  • 1
    What about `app.use(express.urlencoded());`? – J. Chen Jul 20 '17 at 22:59
  • i don't know how to read the data in node, but the data is in the POST body, encoded in the application/x-www-urlencoded format. if you find the request body, see here for parsing it https://stackoverflow.com/a/8486188/1067003 – hanshenrik Jul 20 '17 at 23:04
  • @J.Chen I just added `app.use(express.urlencoded());` and it didn't make a difference :( Also @hanshenrik I'm trying to find `application/x-www-urlencoded` in the POST body but `req.body` is an empty object `{}` – Katie Jul 20 '17 at 23:09
  • Hm not sure where to go from here :/ Does it make any difference if you format payload as object rather than querystring? – J. Chen Jul 20 '17 at 23:14
  • @J.Chen Didn't make a difference :'( thank you for helping me debug, I think I'm not reading it correctly in nodejs or something... I'm trying to find where my missing parameters are hiding – Katie Jul 20 '17 at 23:25
  • 1
    @J.Chen I got it to work now! You were right, the object method helped! I removed the two `-H` options in the CURL command, and changed it to JSON form, then parsed the JSON on the server side! Works now, thank you so much ^-^ – Katie Jul 20 '17 at 23:42
  • Happy to help :) – J. Chen Jul 20 '17 at 23:54

2 Answers2

3

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 to be a JSON object (like {'key':'value',...}) instead of a string (key=value&...),

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; 
    ...
});
Katie
  • 45,622
  • 19
  • 93
  • 125
  • This is quite old, but hopeful it helps: I had to change the quotes: This did not:`data="{'content':'2022'}"`, but this did it: `data='{"content":"2022"}'` – Timo Jan 09 '22 at 17:57
1

Try adding the json body parser middleware

const bodyParser = require('body-parser');
app.use(bodyParser.json());
pizzarob
  • 11,711
  • 6
  • 48
  • 69