用Restifyparsingurl编码体

我无法使用restify对我的node.js API执行url编码的post。 我有我的restify应用程序的以下设置:

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

但是,当我要求我的应用程序使用curl与下面的请求:

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

我在我的视图中获得以下input正文:

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

提前致谢,

马蒂亚斯

Restify的默认设置将parsing的参数放置在req.params 。 这是由queryParser和不同的bodyParser中间件完成的。

因此,要访问quantity参数,请使用req.params.quantity

如果你真的想使用req.body ,你需要传递mapParams : falsebodyParser构造函数:

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

现在req.body将包含parsing的参数。