使用Restify解析url编码的主体

Mat*_*yhr 5 httprequest node.js restify

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

app.use(restify.acceptParser(app.acceptable));                                  
app.use(restify.queryParser());                                                 
app.use(restify.urlEncodedBodyParser());
Run Code Online (Sandbox Code Playgroud)

但是,当我使用curl请求我的应用程序时,请求:

curl -X POST -H "Content-type: application/x-www-form-urlencoded" -d quantity=50 http://app:5000/feeds
Run Code Online (Sandbox Code Playgroud)

我在视图中得到以下输入体:

console.log(req.body)  // "quantity=50"
Run Code Online (Sandbox Code Playgroud)

提前致谢,

马蒂亚斯

rob*_*lep 13

Restify的默认设置将解析后的参数放入req.params.这是由queryParser不同的bodyParser中间件和不同的中间件完成的.

所以要访问quantity参数,请使用req.params.quantity.

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

app.use(restify.plugins.urlEncodedBodyParser({ mapParams : false }));
Run Code Online (Sandbox Code Playgroud)

现在req.body将包含已解析的参数.