使用application/x-www-form-urlencoded使用node.js在post请求中发送数组

Send Array in post request using node.js using application/x-www-form-urlencoded

本文关键字:使用 请求 数组 post application x-www-form-urlencoded node js      更新时间:2023-09-26

我试图向API发送post请求,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}]

注意:标题应包含"内容类型":"application/x-www-form-urlencoded",而不是"application/json"

有人能解决这个问题吗?

如果您阅读了请求的手册,这很容易。您所要做的就是用querystring而不是object替换表单,在您的情况下,这应该是:

amounts=15&amounts=30

我唯一不确定的是,上面的表达式在您的web服务器中是否有效。正如我所知,它在java struts中运行良好。所以如果没有,你可以试试CCD_ 7。希望能有所帮助。