UrlFetchApp 上的 500 错误 [英] 500 error on UrlFetchApp

查看:45
本文介绍了UrlFetchApp 上的 500 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pass data of a product list from Magento API to Google Spreadsheet. No authentication was required for the Magento API as I was retrieving the data as a Guest. The API is working perfectly with RestClient.

但是,从 Googe Apps Script 获取 REST 资源时出现 500 错误.

However, 500 error occurred when fetching the REST resource from Googe Apps Script.

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 脚本:

This is my Google Apps Script:

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++;
}

有什么想法吗?

推荐答案

嘿,诀窍是将以下标题添加到您的请求中

Hey the trick is to add the following headers to your request

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.

Believe the key params for Magento not to return 500 "Service temporary unavailable" are the Content-Type and Accept headers but all params mentioned in example are useful, YMMV.

这篇关于UrlFetchApp 上的 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆