Fetch the title of a web page using URL in a server script

Prince Mendoza
Kilo Expert

I'm trying to create a function that will fetch the title of a web page given the URL.

 

My initial idea is to use the GlideHTTPRequest API using some sort of the following:

 

var url = 'https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172'; //Sample URL
var request = new GlideHTTPRequest(url);
var response = request.get();
gs.print(response.getBody());

 

However, I'm getting the HTML using this method.

 

I would appreciate any pointers to how I should approach this problem.

1 ACCEPTED SOLUTION

Prince Mendoza
Kilo Expert

Something like this seem to work but I am open for other suggestions:

 

 

var url = 'https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172'; //Sample URL
var request = new GlideHTTPRequest(url);
var response = request.get();
var html = response.getBody().toString();
var title = html.slice(html.indexOf('<title>')+7,html.indexOf('</title>'));
gs.print(title);

 


Note: If you use this code for pages in ServiceNow Community, you get "Bounce SSL" as title due to the redirect but it works for other web pages.

View solution in original post

1 REPLY 1

Prince Mendoza
Kilo Expert

Something like this seem to work but I am open for other suggestions:

 

 

var url = 'https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172'; //Sample URL
var request = new GlideHTTPRequest(url);
var response = request.get();
var html = response.getBody().toString();
var title = html.slice(html.indexOf('<title>')+7,html.indexOf('</title>'));
gs.print(title);

 


Note: If you use this code for pages in ServiceNow Community, you get "Bounce SSL" as title due to the redirect but it works for other web pages.