0

I've been trying to use GAS's URLFetchApp to connect to an external website. However, the website redirects me to a "select country" page with a HTTP 302 error no matter what web address I use.

var options = {
    'muteHttpExceptions':true
  }; 
  var response = UrlFetchApp.fetch("http://www.idtdna.com/site/account", options);
  Logger.log(response.getAllHeaders());
  Logger.log(response.getContentText());
  var cookie = response.getAllHeaders()['Set-Cookie']+', CN=US&DateSet=7/28/2015 5:48:55 PM; Path=/; Domain=www.idtdna.com';
  Logger.log(cookie);
  var header = {'Cookie':cookie};
  var o2 = {
    'headers':header,
    'muteHttpExceptions':true
  };
  var r2 = UrlFetchApp.fetch("https://www.idtdna.com/country.aspx", o2);
  Logger.log(r2.getAllHeaders());
  Logger.log(r2.getContentText());

Here is my code. I've been trying to send the cookie but either I've formatted it wrong or am sending it incorrectly because I keep getting redirected regardless.

Qsik Kim
  • 5
  • 3

1 Answers1

0

After setting the muteHttpExceptions option to 'true' and further investigating the website you mentioned, I think it all happens because of the cookies for the website.

So when we first access the website, there is no valid cookie. SO it creates a session and sets the cookie. THis might store the session details with the country as well.

You can try setting the cookie while fetching the url by passing the cookie values in the request header. Check this page for details on how to send the cookie through header.

Hope that helps!

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14