Outbound REST API help

ClareM
Tera Expert

Hi,

 

I have been following the this article which has been really useful to create a REST Message: use-bearer-token-for-rest-message-authentication 

 

The first REST Message is working and I am getting the token in the log message however the second REST Message is returning a 400 status code "Bad Request" response.

 

When I've tested the Rest Message with test values as the variable substitutes it's working fine and I'm getting a 200 status code with the expected response so I assume there is an issue with the script include.

 

I've checked that companyName, account, sortcode, type and token are all returning the correct values in a log message.

 

I've attached my script include code & REST Message and this is my script include code:

 

var BankDetailsCheck = Class.create();
BankDetailsCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getToken: function() {
 
        try {
            var rm = new sn_ws.RESTMessageV2('bankDetailsChecker', "authenticate");
            var response = rm.execute();
            var responsebody = response.getBody();
            var httpStatus = response.getStatusCode();
 
            var a = JSON.parse(responsebody);
            var token = a.token;
            gs.log('token: '+token);
            return token;
        } catch (ex) {
            var message = ex.getMessage();
            gs.error("bankChecker:Token:Error: " + message);
        }
    },
 
retrieveBankCheckResult: function() {
var token = this.getToken();
        var companyName = this.getParameter('sysparm_companyName');
        var sortcode = this.getParameter('sysparm_sortcode');
        var account = this.getParameter('sysparm_account');
var type = this.getParameter('sysparm_type');
 
        var request = new sn_ws.RESTMessageV2('bankDetailsChecker', "checkBankDetails");
request.setStringParameter('token', token);
request.setStringParameter('customerName', companyName);
request.setStringParameter('bankAccount', account);
request.setStringParameter('sortCode', sortcode);
request.setStringParameter('accountType', type);
 
        try {
            var response = request.execute();
var httpStatus = response.getStatusCode();
            gs.log('BankDetailsCheck retrieveBankCheckResult httpStatus:' + httpStatus);
var responseBody = response.getBody();
gs.log('BankDetailsCheck retrieveBankCheckResult responseBody:' + responseBody);
var a = JSON.parse(responseBody);
var result = a.nameMatchResult;
            
        } catch (ex) {
            var message = ex.message;
            gs.log('BankDetailsCheck retrieveBankCheckResult message:' + message);
        }
 
return result;
    },
    type: 'BankDetailsCheck'
 
REST Message.png

 

Can anyone give me any pointers?

 

Thanks,

 

Clare

14 REPLIES 14

Thanks for your help.

 

I've solved by setting the Request Header and Body in the script include instead of in the REST Message using setStringParameter:

 

request.setRequestHeader('Authorization','Bearer ' + token);
//Create payload (JSON with key-value pair)

var myObj = {
"customerName": companyName,
"bankAccount": account,
"sortCode": sortcode,
"accountType": type
} ;

//Convert the object to string and set it to Request Body-
request.setRequestBody(JSON.stringify(myObj));

@ClareM 

Glad to know.

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

@ClareM 

Thank you for marking my response as helpful.

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

Akiladevi Raje1
Giga Guru

@ClareM   Recent servicenow versions allowing massl/mutual authentication+oauth setup, you can create application registry,check the mutual authentication checkbox and choose the right profile..If you have all these setup, then no need to pass the token through script. Below reference configuration setup

AkiladeviRaje1_0-1746012668884.png

Hi Akiladevi,

 

I did have a look at the OAuth authentication however it appeared complicated to set up and I wasn't sure how it would work with the bearer token