使用Restify解析url编码的正文

问题描述 投票:5回答:1

我无法使用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"

谢谢,

马提亚

node.js httprequest restify
1个回答
13
投票

Restify的默认设置将已解析的参数放置在req.params中。 queryParser和不同的bodyParser中间件都可以完成此操作。

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

如果确实要使用req.body,则需要将mapParams : false传递给bodyParser构造函数:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.