javascript - 在 Google Apps 脚本中获取 NWS API 500 错误

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

我正在尝试从 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"
}

如果我从网络浏览器访问此 URL 或 another service ,它工作得很好。救命!

最佳答案

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

  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);

关于javascript - 在 Google Apps 脚本中获取 NWS API 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694690/

相关文章:

javascript - 如何将字符串从 swift 传递到 javascript

javascript - dijit/form/numbertextbox 的设置值设置 displayedValue -- 而不是 value

javascript - 在 Google 表格中编辑历史记录

user-interface - 如何使用应用程序脚本在 Google 电子表格的单元格内添加 UI?

javascript - Google Apps 脚本中的数组 "includes"方法失败

javascript - 如何在R Shiny数据表中设置列宽?

javascript - 如何在gridfs-stream中使用monk启动的mongoDB实例?

java - 为什么我的 REST API 在嵌套对象上返回空白 JSON?

c# - 用对象数组修补对象

javascript - 使用express和body-parser在服务器端解析AngularJS http.post数据