1

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?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Ameerudheen.K
  • 657
  • 1
  • 11
  • 31

1 Answers1

4

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"}
      };
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • That works fine. here whats the difference between `"contentType" : "application/json"` and `"headers":{"Accept":"application/json"}` ? I think both specifed the result type – Ameerudheen.K Oct 13 '19 at 15:03
  • 1
    @Ameer The former specifies the type of content you're **sending**, while the latter specifies the type of content you're able to **accept/receive**. The former is actually not needed in this case as you aren't sending any data(except as query parameters, which if sent as post, it should be **application/x-www-form-urlencoded** and not json) – TheMaster Oct 13 '19 at 16:30