500 error on UrlFetchApp
Asked Answered
W

1

1

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?

Wager answered 27/3, 2015 at 12:7 Comment(4)
ATM it's not working over here, using hurl.it.Bombardon
Thanks but I want to get it worked with Google Apps Script.Wager
I know, but to make it work with ApsScript I have to make it work in a dedicated curl site before, I always use that one. Did you checked in any other Curl site if the address "mymagentohost/api/rest/products?limit=2" is working? Also, google doens't like non secure http links.Bombardon
I checked and it worked in hurl.it!Wager
N
7

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.

Noreen answered 29/3, 2017 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.