0

I am trying to fetch data from an API. I am able to call the API from postman but when I try to call this from Google App Script I am getting no data. I don't get any response even after 1 min and ultimately it times out. Following is my code. Can anybody help me track the issue?

function myFunction() { 
 var opt ={
    "method":"GET",
   "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
    "accept-language": "en-US,en;q=0.9",    
    "followRedirects": false,
   'muteHttpExceptions': true,
   'accept-encoding': 'gzip, deflate, br',
   'cookie': '_ga=GA1.2.1001788100.1597808220; _gid=GA1.2.2063955289.1607778290; nsit=4RM_Oh6erYvr4YPS_n4CN7vk; ak_bmsc=CD2083BC920E78C3D86C7F69BAFFAAF06011B68D521400008E4DD65FD5A6905E~plJFNfSOzIP8W+3MutjCYfLJuMfo7+QwR5GXtkUmc8bTOwOxtK/SQ9l029moqlrX/JWk0ds0rYR5i6ZOQK+pIRy673it3PDgtnbOevcCB9VakNxo59IZSdaQmIn3A8osFVkH0LH1RYeeigds6/If10inS8OGRscyIVkQIkxQYIGmMvupoNX8UgZ+BphtwRFDqkOSOiCRe/TSZRH8AK9/meUk58bConk4VRQepynCxSqiexjGOk0Agd6qqobGhzD58W; nseappid=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcGkubnNlIiwiYXVkIjoiYXBpLm5zZSIsImlhdCI6MTYwNzg4MzMwOCwiZXhwIjoxNjA3ODg2OTA4fQ.T48fqcbru4IWbjYh_oGJqXlRRHglM8TNfXI9-_n8iLw; _gat_UA-143761337-1=1; RT="z=1&dm=nseindia.com&si=ce7cafcc-d516-4a6c-a813-a3f417dc755e&ss=king44kw&sl=4&tt=3fn&bcn=%2F%2F684d0d3d.akstat.io%2F&ld=3cus"; bm_sv=57CE5C9C62EF8E9B2C9B39F1BBBB339F~Veo2CLd/zFZyQo+ewos9TTuguUlHl81MyE6n8wX4Byq1Fd7H8VQRzpFkbp+fh1Xn8kcWIPshipgCMHXiCtInyoacx+WNu0V1Ugoi0ATMpi7O+ny3ppkRXH9yGO2IrVc9dp1HgjXeNViJflQCRjpfdt4rXpo87STgHx0NP5xSgcw=',
   'authority': 'www.nseindia.com',
   'path': '/api/option-chain-indices?symbol=NIFTY',
   'scheme':'https',
   'dnt': '1',
   'referer': 'https://www.nseindia.com/market-data/option-chain',
   'sec-ch-ua-mobile': '?0',
   'sec-fetch-dest': 'empty',
   'sec-fetch-mode': 'cors',
   'sec-fetch-site': 'same-origin'
  };
  
  var url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY";
  var response = UrlFetchApp.fetch(
      url, opt);
  
  
  var json = response.getContentText();
  console.info(json);

}
Venkata Shivaram
  • 343
  • 4
  • 18
JITU
  • 91
  • 1
  • 3

1 Answers1

0

Here is the code for the cUrl which works

curl -v --output test.gz -H User-Agent: -H Accept: -H "Connection: keep-alive" -H "Accept-Encoding: gzip, deflate, br" "https://www.nseindia.com/market-data/option-chain"

the output is stored in test.gz, extract the data from this gzip file.