Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Google AppsScript method is used to get the URL of a redirect?

'www.mysite.com/mySecretKey1' redirects to 'www.othersite.com/mySecretKey2'

in G.AppsScript:

  var response = UrlFetchApp.fetch("https://www.mysite.com/mySecretKey1");
  var headerString = response.getAllHeaders().toSource();
  Logger.log(headerString);
  //string 'www.othersite.com.my/SecretKey2' is not present in log.

How would the script discover the URL address that it is redirected to (i.e. the string 'www.othersite.com/mySecretKey2')?

UPDATE: More generally, how would the script discover the URL address from response?

like image 474
user3645994 Avatar asked Nov 24 '14 05:11

user3645994


3 Answers

Expounding on the answer by Joseph Combs, here's a version that uses recursion to follow multiple redirects, returning only the ultimate canonical URL:

function getRedirect(url) {
  var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
  var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
  var responseCode = response.getResponseCode();
  if (redirectUrl) {                                   // ...if redirected...
    var nextRedirectUrl = getRedirect(redirectUrl);    // ...it calls itself recursively...
    Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
    return nextRedirectUrl;
  }
  else {                                               // ...until it's not
    Logger.log(url + " is canonical. (" + responseCode + ")");
    return url;
  }
}  

function testGetRedirect() {
  Logger.log("Returned: " + getRedirect("http://wikipedia.org"));
}

This logs:

https://www.wikipedia.org/ is canonical. (200)
https://wikipedia.org/ is redirecting to https://www.wikipedia.org/. (301)
http://wikipedia.org is redirecting to https://wikipedia.org/. (301)
Returned: https://www.wikipedia.org/
like image 183
Chris Avatar answered Oct 14 '22 01:10

Chris


There is a native support in UrlFetchApp to follow redirects. You should try to set:

followRedirects = true

In the options you providing to UrlFetchApp. Something like that:

var options = {
   "followRedirects" : true
 };
var result = UrlFetchApp.getRequest("http://your-url", options);
like image 27
Ido Green Avatar answered Oct 14 '22 03:10

Ido Green


UPDATE: More generally, how would the script discover the URL address from response?

Counterintuitively, you need to disable redirection and not mute HttpExceptions, like so:

var followedPost = UrlFetchApp.fetch(properUrl, {'followRedirects': false, 'muteHttpExceptions': false});
Logger.log(followedPost.getHeaders()['Location']);

The object returned by .getHeaders() will contain the new location of the resource being requested. Access that new location with a new .fetch().

like image 20
Joseph Combs Avatar answered Oct 14 '22 03:10

Joseph Combs