【问题标题】:Google Apps Script doesn't recognize "Host" header in HTTP POST request?Google Apps 脚本无法识别 HTTP POST 请求中的“主机”标头?
【发布时间】:2020-04-07 16:55:23
【问题描述】:

我正在尝试从 Google Apps 脚本查询 ArcGIS Rest API。在 Postman 中构建请求工作得非常好,但是当我将代码放入应用程序脚本时,我遇到了我似乎无法弄清楚的问题。代码如下:

function callEsri () {
  var url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query"
  var params =
      {
  "async": true,
  "crossDomain": true,
  "url": "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query",
  "method": "POST",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "PostmanRuntime/7.20.1",
    "Accept": "*/*",
    "Cache-Control": "no-cache",
    "Postman-Token": "[TOKEN]",
    "Host": "services3.arcgis.com",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "125",
    "Connection": "keep-alive",
    "cache-control": "no-cache"
  },
  "data": {
    "f": "json",
    "where": "CITY_JUR LIKE '%Los Angeles%'",
    "outSr": "4326",
    "outFields": "TRL_NAME,ELEV_FT,CITY_JUR,PARK_NAME,FID"
  }
}
  var response = UrlFetchApp.fetch(url, params);
  var json = response.getContentText();
  var data = JSON.parse(json);
  Logger.log(data);
} 

我得到的错误是:执行失败:属性提供了无效值:标题:主机(第 28 行,文件“代码”)

Google 无法识别此问题的任何原因以及是否有解决此问题的方法?非常感谢任何帮助/建议。

【问题讨论】:

  • 主机是从 url 推断出来的。无需指定。旁注:data 是无效参数。您正在寻找payload。文档中的练习示例。
  • 谢谢。这行得通。我很新,我会多练习。感谢您的帮助。

标签: rest http google-apps-script postman arcgis


【解决方案1】:

正如@TheMaster 已经在 cmets 中指出的那样。您已经在 url 中指定了主机。

你也可以看看URLFetchApp的官方文档。

如果您想在此处的标题中获得更多信息,您可以在该标题上使用 mozilla documentation 以及 the RFC specifying the Host header

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2020-11-03
    • 2019-05-08
    相关资源
    最近更新 更多