【问题标题】:Fetching NWS API in Google Apps Script 500 Error在 Google Apps 脚本 500 错误中获取 NWS API
【发布时间】:2018-02-08 20:59:32
【问题描述】:

我正在尝试从 their new GeoJSON API 获取 NWS 警报,但在通过 Google Apps 脚本获取时收到 500 错误。

function getAlerts() {
  var rawData = UrlFetchApp.fetch('https://api.weather.gov/alerts/active', {muteHttpExceptions: true});
  Logger.log(rawData);
}

日志:

{
  "correlationId": "085cf198-e7ee-427c-8bb4-9bfd9f91fe9f",
  "title": "Unexpected Problem",
  "type": "https://api.weather.gov/problems/UnexpectedProblem",
  "status": 500,
  "detail": "An unexpected problem has occurred.",
  "instance": "https://api.weather.gov/requests/085cf198-e7ee-427c-8bb4-9bfd9f91fe9f"
}

如果我从我的网络浏览器或another service 访问此 URL,它就可以正常工作。救命!

【问题讨论】:

    标签: javascript json google-apps-script http-headers geojson


    【解决方案1】:

    这是标头的问题,您可以阅读documentation(在 API 参考下)应该为每个端点使用哪个标头,然后您只需像这样添加它们:

      var headers = {Accept: "application/ld+json"}
      var options = {"headers": headers};
      var currentWeatherUrl = "https://api.weather.gov/alerts/active?active=1"
      var response = UrlFetchApp.fetch(currentWeatherUrl, options);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多