POST 多个字段同名

POST with multiple fields having the same name

我正在尝试从需要 POST 参数的 API 中收集一些数据

POST /v2/address/addr/ HTTP/1.1
Host: api.omniwallet.org
Content-Type: application/x-www-form-urlencoded
addr=test1&addr=test2

CURL 版本

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Content-Type: application/x-www-form-urlencoded" -d "addr=test1&addr=test2" "https://api.omniwallet.org/v2/address/addr/"

https://api.omniwallet.org/#doc-general-notes

问题是,如您所见,字段具有相同的名称 "addr"。

所以我尝试将数据放在一个名为 "addr"

的数组中
var options = { 'method' : 'POST' ,'payload' : { addr: [ "test1", "test2"] } };
var result = UrlFetchApp.fetch('https://api.omniwallet.org/v2/address/addr/', options);

但没有成功,服务器无法识别列表中的表单数据字段 "addr"。 也试过

var options = { 'method' : 'POST' ,'payload' : { addr: "test1", addr: "test2" } };
var result = UrlFetchApp.fetch('https://api.omniwallet.org/v2/address/addr/', options);

但它也不起作用,因为它只会考虑第二个 "addr" 字段的值(很明显)。

是否有以某种其他方式执行此 POST 方法的方法,我可以像在 CURL 示例中那样强制执行 "addr=test1&addr=test2"?

谢谢!

  • 您想将 curl 命令转换为 Google Apps 脚本。

如果我的理解是正确的,你要使用的服务器需要这些值作为表单数据,那么这个修改怎么样?

修改点:

  • 在您的 curl 命令中,addr=test1&addr=test2 的值以 "addr": ["test1", "test2"] 的形式发送。
    • 在您第一次尝试时,无法解析 addr 的数组。
    • 在您第二次尝试时,由于使用了相同的密钥,仅发送了 addr: "test2"

将以上几点反映到脚本中,修改后的脚本如下

修改后的脚本:

本次修改,修改了options。我认为这与 @TheMaster.

的结果相同
var options = {
  method: 'POST',
  payload: 'addr=test1&addr=test2', // Modified
};

如果这不起作用,我很抱歉。