Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

Unabled to retrieve response body of RestMessage API v2 Request

Subramani3
Giga Contributor

Hi Developers!

 

I am using RestMessgev2 for sending a POST API request to a Third-Party API endpoint in a Scoped Application.   I am able to successfully retrieve the response body when the HTTP Status Code is 200. However, I am receiving ClaimsService.getPrincipal() - Failed to fetch the user - 'ServiceNowTestUser' validation details 
when the HTTP Status Code is 400,403 etc (Failed).

Code Snippet:

var restAPI = new sn_ws.RESTMessageV2('<REST MESSAGE v2 Object Name>', 'GENERATE_ACCESS_TOKEN');

var endPoint = restAPI.getEndpoint();
endPoint = endPoint.replace('${baseUrl}', 'BASE URL GOES HERE');
restAPI.setEndpoint(endPoint);

var
requestBody = "client_id=<CLIENT ID GOES HERE>" + "&client_secret=<CLIENT SECRET GOES HERE>" + "&username=ServiceNowTestUser" + "&password=<PASSWORD GOES HERE>" + "&grant_type=password";       
 
restAPI.setRequestBody(requestBody);

var response = restAPI.execute();
var responseBodyStr = response.getBody();
           
gs.info("Response: "+responseBodyStr) ;
           

It would be really great if you take a look at the issue and provide necessary resolution or workaround.

Thanks in advance!

1 ACCEPTED SOLUTION

yashkamde
Mega Sage

Hello @Subramani3 ,

 

try to get the actual failure reason from the third party:

var restAPI = new sn_ws.RESTMessageV2('<REST MESSAGE v2 Object Name>', 'GENERATE_ACCESS_TOKEN');

var endPoint = restAPI.getEndpoint().replace('${baseUrl}', 'BASE URL GOES HERE');
restAPI.setEndpoint(endPoint);

var requestBody = "client_id=<CLIENT ID>" +
    "&client_secret=<CLIENT SECRET>" +
    "&username=ServiceNowTestUser" +
    "&password=<PASSWORD>" +
    "&grant_type=password";

restAPI.setRequestBody(requestBody);

try {
    var response = restAPI.execute();
    var statusCode = response.getStatusCode();
    var responseBody = response.getBody();

    if (statusCode == 200) {
        gs.info("Success: " + responseBody);
    } else {
        gs.error("Third-party call failed. Status: " + statusCode +
                  " | Body: " + responseBody +
                  " | ErrorMsg: " + response.getErrorMessage());
    }
} catch (ex) {
    gs.error("RESTMessageV2 exception: " + ex.getMessage());
}

 

 

If my response helped mark as helpful and accept the solution.

View solution in original post

2 REPLIES 2

yashkamde
Mega Sage

Hello @Subramani3 ,

 

try to get the actual failure reason from the third party:

var restAPI = new sn_ws.RESTMessageV2('<REST MESSAGE v2 Object Name>', 'GENERATE_ACCESS_TOKEN');

var endPoint = restAPI.getEndpoint().replace('${baseUrl}', 'BASE URL GOES HERE');
restAPI.setEndpoint(endPoint);

var requestBody = "client_id=<CLIENT ID>" +
    "&client_secret=<CLIENT SECRET>" +
    "&username=ServiceNowTestUser" +
    "&password=<PASSWORD>" +
    "&grant_type=password";

restAPI.setRequestBody(requestBody);

try {
    var response = restAPI.execute();
    var statusCode = response.getStatusCode();
    var responseBody = response.getBody();

    if (statusCode == 200) {
        gs.info("Success: " + responseBody);
    } else {
        gs.error("Third-party call failed. Status: " + statusCode +
                  " | Body: " + responseBody +
                  " | ErrorMsg: " + response.getErrorMessage());
    }
} catch (ex) {
    gs.error("RESTMessageV2 exception: " + ex.getMessage());
}

 

 

If my response helped mark as helpful and accept the solution.

Thank you so much for the reply. I am trying to get a 400 with an incorrect username and so that I can test the validation flow. I am not able to get the actual response which is sent by the API endpoint for 400.

When I try the API request in Postman with username incorrect intentionally then I am getting a 400 status code with the below details in the body. I am trying to retrieve the value in ServiceNow but unable to do so.

{
    "error": "invalid_request",
    "error_description": "The username and password you entered do not match an account in this organization."
 
I had also tried using the code which you shared and below is the output that I am receiving for getErrorMessage()

Method failed: (/AuthServices/Auth/connect/token) with code: 400