Validating API responses

Creating assertions in the API Test Editor

When creating an API test, it is important to add assertions on the response of individual API requests to validate that the API returns the expected result. For example, you may want to assert that the status code of the request equals 200 and that the response body (JSON) contains a user with a specific email address.

📘

Default status assertion

By default, when you create a new API step in your test, mabl will automatically create an assertion that expects the status code to be equal to 200. This assertion can be edited or removed as needed.

Creating assertions without code

You can easily create assertions based on the following response data:

  • Status code
  • Size
  • Headers
  • Body

Supported assertion types are:

  • Equals
  • Does not equal
  • Contains
  • Does not contain
  • Is present

To create an assertion, take the following steps:

  1. In the Validation and variable assignment section, click on the Assertions tab.
  2. Click on the Add button. An assertion card will appear.
  3. Enter a description for your assertion.
  4. Select an assertion target. If you select Header, enter the header key. For a JSON body, type the JSON body path to the target response property.
  5. Select an assertion type and specify the expected value.

📘

Response body validation

Only the JSON format is supported for asserting on the response body without writing JavaScript. If you need to validate SOAP-based APIs that work with XML response body, you can use JavaScript code as described in the API script examples guide.

To test out your assertions, click on the Send button. In the Results section, click on the Assertions tab to check whether the assertion passed.

1144

Creating an assertion on the response from an API request

Editing assertions

To edit an existing assertion, take the following steps:

  1. Open the Assertions tab in the Validation and variable assignment panel.
  2. Click on the pencil icon next to the assertion.
  3. Make updates to the assertion as needed.
  4. Click Save to confirm the updates to the assertion.

If you need to handle more complex validation scenarios, you can use JavaScript code as described in the API script examples guide.