500 error on UrlFetchApp

被刻印的时光 ゝ 提交于 2021-02-10 15:16:18

问题


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.

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) 

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

Any ideas?


回答1:


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

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.



来源:https://stackoverflow.com/questions/29300298/500-error-on-urlfetchapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!