Testing Redirects using Google Spreadsheets and Google Apps Script

So after just few seconds (9.160.261) my second post…

During one project I had to redirect a lot of urls,  so I created a tool to help me to check them quickly.
I created a Google Spreadsheet adding some extra power using a bit of Google Apps Script.
Here it’s: https://docs.google.com/spreadsheet/ccc?key=0Ag4zrVLfCksydG1ZRlNlREgwME1Zb1BHYmxUNjU4MHc#gid=0

It works using 2 simple functions: statuscode & location

The first one check the status code of the requested url, the second one extracts the location from the header.

Here it’s the code:

function statuscode(url, user, pwd) {
    try {
        var response = UrlFetchApp.fetch(url, {
            muteHttpExceptions: true,
            followRedirects: false,
            headers: {
                'Authorization': 'Basic ' + Utilities.base64Encode(user+':'+pwd)
            }
        });
        return response.getResponseCode();
    } catch (error) {
        
        return "Error";
    }
}



function location(url, user, pwd) {
    try {
        var response = UrlFetchApp.fetch(url, {
            muteHttpExceptions: true,
            followRedirects: false,
            headers: {
                'Authorization': 'Basic ' + Utilities.base64Encode(user+':'+pwd)
            }
        });
        header = response.getHeaders();
        location = header['Location'];
        return location;
    } catch (error) {
        return "Error";
    }
}

Then the spreadsheet check if the desired redirect (column B) is it equal to the location set in the header (column D). You can also add a User (column F) and Password (column G) to test URL protected by basic authentication.

So if you find it useful just duplicate the spreadsheet and use it as you need it.