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

@Ankur Bawiskar I am triggering the script from from a UI Macro. I've checked that the values are being passed to the script include

I am receiving the token

The log for the repsonse body is {"message":"Bad Request","details":"Please check the details provided. Error Code: 1026. Request Id: 102042576-2025-04-30T114432-aceb1e6826e44b9fa11f76."}

I've printed the request body in the log and it looks like the variables are not being substituted 

requestBody {
"customerName": "${companyName}",
"bankAccount": "${account}",
"sortCode": "${sortcode}",
"accountType": "${type}"
}

@ClareM 

so that's the issue.

Because the variable substitution didn't work it didn't post the correct request body to API

are these values received correctly in script include from that UI macro via GlideAJax?

var companyName = this.getParameter('sysparm_companyName');
var sortcode = this.getParameter('sysparm_sortcode');
var account = this.getParameter('sysparm_account');
var type = this.getParameter('sysparm_type');

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

@Ankur Bawiskar Yes I've checked that those values are being received correctly in the retrieveBankCheckResult: function via a log message

@ClareM 

the way how variables are substituted seems incorrect.

Did you copy the code from Preview Script Usage?

Use the exact one

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 

Did you try to test the REST Message in background script and hard-code those variables and test?

If it doesn't work then it's not issue with the logic, it's the issue with how the 3rd party expects the request body

You are sending wrong request body then

please check the API specs from 3rd party team which they shared

       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;
           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);
           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;
           gs.info(result);
       } catch (ex) {
           var message = ex.message;
           gs.log('BankDetailsCheck retrieveBankCheckResult message:' + message);
       }

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