使用application / x-www-form-urlencoded使用node.js发送数据

我试图发送post请求到API和post参数应该是数组,这是如何发送它在cURL

curl http://localhost:3000/check_amounts -d amounts[]=15 \ -d amounts[]=30 

我试图在Node.js中使用请求模块来做到这一点

 request.post('http://localhost:3000/check_amounts', { form: { 'amounts[]': 15 , 'amounts[]': 30 } }, function(error, response, body) { console.log(body) res.json(body); }); 

但第二个数量覆盖第一个数量,API得到的结果如下: amounts = [30]

然后我尝试以不同的方式发送

  request.post('http://localhost:3000/check_amounts', { form: { 'amounts[]': [ 15 , 30] } }, function(error, response, body) { console.log(body) res.json(body); }); 

但结果不是预期的amounts = [{"0":15},{"1":30}]

注意:标题应该包含'Content-Type':'application / x-www-form-urlencoded'而不是'application / json'

有没有人可以解决这个问题?

如果您阅读请求手册,这很容易。 所有你应该做的是用querystring而不是object来replace表单,在你的情况下这应该是:

amounts=15&amounts=30

唯一不能确定的是上面的expression式在你的web服务器上工作。 据我所知,它在java struts运行良好。 所以,如果没有,你可以试试amounts[]=15&amounts[]=30 。 希望它有帮助。