I am working on verifying 301 redirects are functioning properly. I have a Google Sheet of URLs and have a Google script that checks the status being returned:
function getStatusCode(url){
var options = {
'muteHttpExceptions': true,
'followRedirects': false
};
var url_trimmed = url.trim();
var response = UrlFetchApp.fetch(url_trimmed, options);
return response.getResponseCode();
}
Next I am wanting to verify that the URL is being redirected correctly. This is where I'm getting stuck. I tried using a variation of the above code, but I can just return the URL being passed in, not the URL being redirected to, or I just get an error on my Google Sheet. Here is the last bit I tried using (that returns an error).
function getReturnedURL(url) {
var options = {
'method': 'GET',
'muteHttpExceptions': true,
'followRedirects': true
};
var url_trimmed = url.trim();
var returnedUrl = ScriptApp.getService().getUrl(url_trimmed);
var response = UrlFetchApp.fetch(returnedUrl, options);
return response;
}
Any ideas? Or, is this even possible?
ScriptApp.getService().getUrl(url_trimmed). BecausegetUrl()has no arguments. Ref About this line, can you explain about what you want to do? - Tanaike.getUrl();function and thought I read somewhere I could pass it in a value. TheScriptApp.getService().getUrl();was returningnullfor me, so I was trying to pass it in the URL I was trying to retrieve. - Billy