我无法使用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
将包含已解析的参数。