The CreatorCon Call for Content is officially open! Get started here.

Need Assistant with Basic Athentication

Community Alums
Not applicable

Hi All, I have one email script to generate clickable link below code is a part of email script, Sometimes link gives an error ("414 Request - URI Too Large") error, As per checking URL length is too long approx 15k to 17k characters thats why that error came.

To fix this error I used below code, It is working fine, It converts Long url to tiny url by using POST method and create record in sys_tiny_url table and then used that tiny url in our email, It looks good and working as expected, but Hardcoding username / passwd in a script is not a good idea, Can anyone please suggest how can I proceed without hadrcoading of username and password in script

 

 

 

 //Maximum Practical length is 2086 charecters
    if (url.length < 2086) {

         gs.info('No need to convert in Tiny URL is');

        template.print('<a href=' + url + '>report link </a');

    } else {

        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
        request.setHttpMethod('POST');

        //Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
        var user = "admin";
        var password = "admin";

        var body = {};
        body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


        request.setBasicAuth(user, password);

        request.setRequestHeader("Accept", "application/json");

        request.setRequestHeader('Content-Type', 'application/json');

        request.setRequestBody(JSON.stringify(body));

        var response = request.execute();

        var jsonString = response.getBody().toString(); // get JSON of url

        var cleanedString = jsonString.replace(/["}]+$/g, '');

        var startPos = cleanedString.indexOf('"result":"');
        if (startPos !== -1) {

            var shorturl = cleanedString.substring(startPos + '"result":"'.length);

            
        }
         gs.info('Tiny URL is: ' + shorturl);

        template.print('<a href=' + shorturl + '>report link </a');

    }

 

 

1 ACCEPTED SOLUTION

@Community Alums 

setup the profile in REST Message then inside your Rest message function set the authentication type as Inherit from parent or you can again select the same basic auth profile

add the profile name which you create as 2nd parameter

//Maximum Practical length is 2086 charecters
if (url.length < 2086) {

gs.info('No need to convert in Tiny URL is');

template.print('<a href=' + url + '>report link </a');

} else {

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
// var user = "admin"; // not required
//var password = "admin"; // not required

var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


//request.setBasicAuth(user, password); // this is not required

request.setAuthenticationProfile('basic', profile name); // give the basic auth profile name here

request.setRequestHeader("Accept", "application/json");

request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(JSON.stringify(body));

var response = request.execute();

var jsonString = response.getBody().toString(); // get JSON of url

var cleanedString = jsonString.replace(/["}]+$/g, '');

var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {

var shorturl = cleanedString.substring(startPos + '"result":"'.length);


}
gs.info('Tiny URL is: ' + shorturl);

template.print('<a href=' + shorturl + '>report link </a');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

17 REPLIES 17

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

is there any other way to use this rest message without username and password?

@Community Alums 

you can use basic auth profile and there you can add username and password

no need of these lines, the rest message will automatically use those credentials while hitting the API endpoint

AnkurBawiskar_0-1699614372297.png

 

AnkurBawiskar_1-1699614396480.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

How I use this in my existing email script?

@Community Alums 

setup the profile in REST Message then inside your Rest message function set the authentication type as Inherit from parent or you can again select the same basic auth profile

add the profile name which you create as 2nd parameter

//Maximum Practical length is 2086 charecters
if (url.length < 2086) {

gs.info('No need to convert in Tiny URL is');

template.print('<a href=' + url + '>report link </a');

} else {

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
// var user = "admin"; // not required
//var password = "admin"; // not required

var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


//request.setBasicAuth(user, password); // this is not required

request.setAuthenticationProfile('basic', profile name); // give the basic auth profile name here

request.setRequestHeader("Accept", "application/json");

request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(JSON.stringify(body));

var response = request.execute();

var jsonString = response.getBody().toString(); // get JSON of url

var cleanedString = jsonString.replace(/["}]+$/g, '');

var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {

var shorturl = cleanedString.substring(startPos + '"result":"'.length);


}
gs.info('Tiny URL is: ' + shorturl);

template.print('<a href=' + shorturl + '>report link </a');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

what will be the END Point in REST Message?