javascriptgoogle-apps-scriptgoogle-sheets

TypeError: UrlFetchApp.fetch(...).then is not a function


I am trying to make an API call from this website to my google sheet but getting this error. Can someone guide me in the right direction.

Thanks in advance

function myFunction() {

var myHeaders = {
     "Accept":"application/JSON, text/javascript, */*;q=0.01"
}; 

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  redirect: 'follow'
};

var response = UrlFetchApp.fetch("http://vixcentral.com/ajax_historical?n1=2023-10-17", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
  
}

Solution

  • In your script, how about the following modification?

    Modified script:

    function myFunction() {
      var myHeaders = {
        "Accept": "application/JSON, text/javascript, */*;q=0.01"
      };
      var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        muteHttpExceptions: true,
      };
      var response = UrlFetchApp.fetch("http://vixcentral.com/ajax_historical?n1=2023-10-17", requestOptions);
      var res = response.getContentText();
      if (response.getResponseCode() != 200) {
        console.error(res);
      }
      console.log(res);
    }
    

    Reference: