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 validate information from the response body, such as a success flag or message, as well as HTTP response properties such as the status code.
Validate a Response Using JavaScript
The following example parses the response body and throws an error if the API indicates that the operation was not successful:
var $response = post($url, $payload);
var $jsonList = JSON.parse($response.getDataString());
if ($jsonList.isSuccess !== "true") {
throw "Call was not successful. Message: " + $jsonList.message;
}
In this example:
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
Instead of using an if statement, you can use an assertion to validate a value in the response:
var $response = post($url, $payload);
var $jsonList = JSON.parse($response.getDataString());
assertEquals($jsonList.isSuccess, "true");
The assertion fails if isSuccess does not equal "true".
Validate the HTTP Status Code
You can also validate the HTTP 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 allows you to verify both:
- The HTTP request returned the expected status code.
- The API 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 code returned by the server.
Using these validations helps ensure that API requests not only execute successfully but also return the expected results.
Last updated: September 1, 2026
If you have any feedback on this topic, please contact us at documentation@appvance.ai.