Search code examples
google-apps-scriptgoogle-sheetsurlfetchquandl

Quandl API returns HTML response on Google appscript


API request to quandl for fetching stock data returning HTML response instead of JSON. Its correctly returning JSON result in postman.

var url ='https://www.quandl.com/api/v3/datasets/BSE/BOM'+532540+'?start_date='+startDate+'&end_date='+endDate+'&collapse=weekly&api_key=myapikey'
  console.log(url)
  var options =
      {
        'muteHttpExceptions': true,
        "contentType" : "application/json",
      };
  var response = UrlFetchApp.fetch(url, options);
  console.log(response)

did anyone have a workaround?


Solution

  • Issue:

    • Xml response instead of JSON response from quandl api

    Solution:

    • Explicitly mention the format in the url as mentioned in the documentation

      GET https://www.quandl.com/api/v3/datasets/{database_code}/{dataset_code}/data.{return_format}
      
      
      var url ='https://www.quandl.com/api/v3/datasets/BSE/BOM'+532540+'.json?start_date='+startDate+'&end_date='+endDate+'&collapse=weekly&api_key=myapikey'
      

    AND/OR

    • Try mentioning that you only accept json response in the request using Accept header.
      var options =
          {
            'muteHttpExceptions': true,
            "contentType" : "application/json",
            "headers":{"Accept":"application/json"}
          };