簡體   English   中英

使用application / x-www-form-urlencoded使用node.js在發布請求中發送數組

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

我嘗試將發布請求發送到API,並且發布參數應為數組,這是如何在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替換表單,在這種情況下,應該是:

amounts=15&amounts=30

我唯一不確定的是上面的表達式是否可以在您的Web服務器中使用。 據我所知,它在java struts效果很好。 因此,如果不是這樣,您可以改為嘗試amounts[]=15&amounts[]=30 希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM