sorry if this is a simple question but I was wondering how I would be able to return the redirected URL of a webpage using google sheets.
Sample input webCode = SNSKS21KK
I've attached the code below:
function CHECKWEBSITE(webCode){
if (webCode.map){
return webCode.map(CHECKWEBSITE);
} else {
var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var redirectedUrl = ?;
var options = {
'muteHttpExceptions': true,
'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var status = response.getResponseCode();
if (redirectedUrl.includes("discontinued") || status == 302) {
return "Not Online";
} else if (status == 200 || status == 301){
return "Online";
} else {
return "Not imported";
}
}
}
Expected redirect output = https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings
Thanks!
In your situation, how about the following modification?
var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var redirectedUrl = ?;
var options = {
'muteHttpExceptions': true,
'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var status = response.getResponseCode();
var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var options = {
'muteHttpExceptions': true,
'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var redirectedUrl = response.getAllHeaders().Location;
var status = response.getResponseCode();
redirectedUrl is retrieved from Location of the response header.webCode = SNSKS21KK, redirectedUrl is https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings and Online was returned.