Validating API responses

When creating your API tests, it is important that you 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.

Creating assertions and variables without code

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. You can change this assertion from Validation and variable assignment section for that API step.

You can add new assertions from the Assertions tab of the Validation and variable assignment section. For example, let's say you want to assert that the response body for an API request contains a user with a specific email address. To create this assertion, click on the Add button. You will see an assertion card with all available options. Select JSON body as a target for your assertion and type the JSON path to the target response property that should contain the specific email address. In our case this is some vision path. It is best to send the API request to see what the path to the target response property should be. Once you specify the JSON path, you can select the assertion type (e.g. equals, contains) and specify the expected value.

Creating an assertion for an API request's response.Creating an assertion for an API request's response.

Creating an assertion for an API request's response.

You can also choose to assign target property value to a variable which can be used in consecutive test steps or shared with other tests within the same test plan. Simply, go to the Variables tab, repeat the steps above to get the target value that you want assigned to a variable and give the variable a name. When you send the request again, you will see the variable appear in the variables section of the left-side panel.

Creating a variable based on the response value.Creating a variable based on the response value.

Creating a variable based on the response 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 Using JavaScript with API tests guide.

Following the above approach, you can create as many assertions and variables as needed for each step of your API test. Thus, when you run the API test, if one of the assertion fails, then the test run will be marked as failed.

You can easily create assertions and variables 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
  • Not present

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


Did this page help you?