Node.js 使用Restify解析url编码的正文
我无法使用restify向node.js API发送url编码的帖子。我的restify应用程序设置如下:Node.js 使用Restify解析url编码的正文,node.js,httprequest,restify,Node.js,Httprequest,Restify,我无法使用restify向node.js API发送url编码的帖子。我的restify应用程序设置如下: app.use(restify.acceptParser(app.acceptable)); app.use(restify.queryParser()); app.use(restify.urlEncodedBodyPars
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"
提前感谢,
MattiasRestify的默认设置将解析的参数放置在
req.params
中。这是由queryParser
和不同的bodyParser
中间件完成的
因此,要访问quantity
参数,请使用req.params.quantity
如果您确实想使用req.body
,则需要将mapParams:false
传递给bodyParser
构造函数:
app.use(restify.plugins.urlEncodedBodyParser({ mapParams : false }));
现在
req.body
将包含解析的参数。当我使用解析器时,我的整个服务对任何URL都没有响应。它永远不会返回,也不需要断点hit@ChristianBongiorno这通常表示中间件没有沿着中间件链传递请求。这个答案很老了,可能是后来发生了一些变化,这可能会导致你所看到的行为。我独立地发现了这一点,但你是对的。CORS模块阻止了整个过程。老实说,restify并没有给我留下深刻的印象。