Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse url encoded body with Restify

I'm not able to do url encoded posts to my node.js API using restify. I have the following setup of my restify app:

app.use(restify.acceptParser(app.acceptable));                                  
app.use(restify.queryParser());                                                 
app.use(restify.urlEncodedBodyParser());

But when I'm requesting my app using curl with the following request:

curl -X POST -H "Content-type: application/x-www-form-urlencoded" -d quantity=50 http://app:5000/feeds

I get the following input body in my view:

console.log(req.body)  // "quantity=50"

Thanks in advance,

Mattias

like image 495
Mattias Farnemyhr Avatar asked Jan 03 '14 10:01

Mattias Farnemyhr


1 Answers

The default setup for Restify places parsed parameters in req.params. This is done by both the queryParser and the different bodyParser middlewares.

So to access the quantity parameter, use req.params.quantity.

If you really want to use req.body instead, you need to pass mapParams : false to the bodyParser constructor:

app.use(restify.plugins.urlEncodedBodyParser({ mapParams : false }));

Now req.body will contain the parsed parameters.

like image 185
robertklep Avatar answered Nov 16 '22 04:11

robertklep