UrlFetchApp fetch method fails with 404 error, but same URL works with Curl and browsers

Viewed 195

If I execute below code, it works fine:

curl 'http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true'

I can also execute this URL from any browser and it returns expected results

http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true

However, below code fails with error 404.

function fetchFromURLTemp() {
  let url = "http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true";
  var options = {
    'method': 'GET',
    'headers': { 'Accept': '*/*' },
    'muteHttpExceptions': true,
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*; q = 0.8'
  };

  let httpResponse = UrlFetchApp.fetch(url, options);
  let status = httpResponse.getResponseCode()
  let urlData = httpResponse.getContentText();
  console.info("Response Status : " + status + ", Response Content : " + urlData)
}

It prints following message on the console

Response Status : 404, Response Content : The report is no longer supported

Can someone please advise what changes I need to get the response.

Thanks!

1 Answers

It works fine for me, just change let to var

 function fetchFromURLTemp() {
  var url = "http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true";
  var options = {
    'method': 'GET',
    'headers': { 'Accept': '*/*' },
    'muteHttpExceptions': true,
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*; q = 0.8'
  };

  var httpResponse = UrlFetchApp.fetch(url, options);
  var status = httpResponse.getResponseCode()
  var urlData = httpResponse.getContentText();
  Logger.log("Response Status : " + status + ", Response Content : " + urlData)
}
Related