How to validate a response?
When executing API requests in Service Suite, you can validate the response to verify that the request was successful.
You can constructvalidate information from the URLresponse ofbody, such as a success flag or message, as well as HTTP response properties such as the endpointstatus usingcode.
Validate a Response Using JavaScript
The concatenationfollowing expressionsexample parses the response body and throws an error if the API indicates that can include variables. So for example in the followingoperation snippetwas thenot variable $clientNumber is used in that path of the URL of a post call:successful:
var $response = post($url, $payloadpayload);
);
var $jsonList = JSON.JSON.parse($response.getDataString());
if ($jsonList.isSuccess !== "true") {
throw "Call was not successful. Message: " + $jsonList.message;
}
In
post()sends the request to the endpoint.$responsecontains the HTTP response.getDataString()retrieves the response body as a string.JSON.parse()converts the JSON response into a JavaScript object.- The
isSuccessproperty is checked to determine whether the API call was successful. - If the call was unsuccessful,
throwstops the execution and displays the message returned by the API.
Validate a Response Using assertEquals
OptionallyInstead of using an if statement, you can also use assertan callsassertion like:to validate a value in the response:
var $response = post($url, $payloadpayload);
);
var $jsonList = JSON.JSON.parse($response.getDataString());
assertEquals($jsonList.isSuccess, "true");
The
isSuccess does not equal "true".
Validate the HTTP Status Code
You can also checkvalidate otherthe response information likeHTTP status code returned by the server:
assertEquals($response.status, 200);
This verifies that the response status is 200 (OK).
For example, you can combine a status-code validation with a response-body validation:
var $response = post($url, $payload);
assertEquals($response.status, 200);
var $jsonList = JSON.parse($response.getDataString());
assertEquals($jsonList.isSuccess, "true");
This
Weyou canto alsoverify useboth:
- The HTTP request returned the
sameexpectedapproachstatusevencode. - The
ifAPI response body contains the expected result.
Summary
Service Suite provides multiple ways to validate API responses:
- Use
throwwhen you want to stop execution with a custom error message. - Use
assertEquals()to validate values returned in the response body. - Use
$response.statusto validate the HTTP status codeisreturnedseenbyintheheaderserver.
Using wellthese validations helps ensure that API requests not only execute successfully but also return the expected results.
assertEquals($response.status, 200);Last updated: MarchSeptember 21,1, 20232026
If you have any feedback on this topic, please contact us at documentation@appvance.ai.