Quantcast
Channel: nemone.it
Viewing all articles
Browse latest Browse all 11

Testing Redirects using Google Spreadsheets and Google Apps Script

$
0
0

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

During one project we 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: link

It works using 2 simple functions: statuscode & location

The first one checks 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.

The post Testing Redirects using Google Spreadsheets and Google Apps Script appeared first on nemone.it.


Viewing all articles
Browse latest Browse all 11

Trending Articles