我无法使用restify对我的node.js API进行url编码的帖子.我有以下设置我的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
我在视图中得到以下输入体:
console.log(req.body) // "quantity=50"
提前致谢,
马蒂亚斯
Restify的默认设置将解析后的参数放入req.params
.这是由queryParser
不同的bodyParser
中间件和不同的中间件完成的.
所以要访问quantity
参数,请使用req.params.quantity
.
如果你真的想要使用req.body
,你需要传递mapParams : false
给bodyParser
构造函数:
app.use(restify.plugins.urlEncodedBodyParser({ mapParams : false }));
现在req.body
将包含已解析的参数.