【问题标题】:500 error on UrlFetchAppUrlFetchApp 上的 500 错误
【发布时间】:2015-03-27 12:07:35
【问题描述】:

我正在尝试将产品列表的数据从 Magento API 传递到 Google 电子表格。 Magento API 不需要身份验证,因为我以访客身份检索数据。 API 与 RestClient 完美配合。

但是,从 Googe Apps 脚本获取 REST 资源时出现 500 错误。

Exception: Request failed for
http://mymagentohost/api/rest/products?limit=2
returned code 500. Truncated server response: Service temporary
unavailable (use muteHttpExceptions option to examine full response) 

这是我的 Google Apps 脚本:

function myscript() {
  var url = "http://mymagentohost/api/rest/products?limit=2"
  var response = UrlFetchApp.fetch(url);

  var out = JSON.parse(response.getContentText());
  var doc = SpreadsheetApp.create("Product Info");
  var cell = doc.getRange('a1');
  var index = 0;
  for (var i in out) {
  var value = out[i];
  cell.offset(index, 0).setValue(i);
  cell.offset(index, 1).setValue(value);
  index++;
}

有什么想法吗?

【问题讨论】:

  • ATM 在这里不工作,使用hurl.it
  • 谢谢,但我想让它与 Google Apps 脚本一起使用。
  • 我知道,但要让它与 ApsScript 一起工作,我必须让它在一个专门的 curl 站点上工作,我总是使用那个。如果地址“mymagentohost/api/rest/products?limit=2”有效,您是否检查过任何其他 Curl 站点?此外,谷歌不喜欢不安全的 http 链接。
  • 我检查过,它在 hurl.it 中有效!

标签: api rest magento google-apps-script


【解决方案1】:

诀窍是将以下标头添加到您的请求中

var url = "http://mymagentohost/api/rest/products?limit=2"

var params = { 
  headers: { 'Content-Type': "application/json", 'Accept': "application/json"},
  muteHttpExceptions: true,
  method: "GET",
  contentType: "application/json",
  validateHttpsCertificates: false,
};

var response = UrlFetchApp.fetch(url, params);

相信 Magento 不返回 500“服务暂时不可用”的关键参数是 Content-Type 和 Accept 标头,但示例中提到的所有参数都是有用的,YMMV。

【讨论】:

  • 谢谢。这对我有用(甚至省略了muteHttpExceptionsvalidateHttpsCertificates 行)。
猜你喜欢
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 2019-09-01
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多