【问题标题】:POST with multiple fields having the same name具有相同名称的多个字段的 POST
【发布时间】:2018-12-10 19:48:18
【问题描述】:

我正在尝试从需要像这样的 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”?

谢谢!

【问题讨论】:

  • 只需发送与有效载荷相同的字符串
  • 也许试试:var options={'method' : 'POST' };var result = UrlFetchApp.fetch('https://api.omniwallet.org/v2/address/addr/?addr=test1&addr=test2', options); 我认为即使是 POST 请求,您也可以在 URL 中添加搜索字符串。
  • 作为 QueryString 发送不起作用。响应是“{“错误”:“字典中没有地址”}”。发送与@TheMaster 建议的有效负载相同的字符串解决了这个问题。
  • @Bruno 太好了。考虑接受下面的解决方案,它说明了同样的事情。

标签: curl google-apps-script urlfetch


【解决方案1】:
  • 您想将 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
};

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2015-01-02
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多