An API that returns 200 OK with an empty body, the wrong customer’s data, or a price of ₹0 has still returned 200. Many beginner API tests stop at the status code, and so they pass happily while the product is broken. A more useful way to think about an API response is as a set of layers, each of which can be wrong independently.
Layer 1: the status code, read precisely
The status code still matters, but check the specific code the contract promises rather than “anything in the 2xx range”. Creating a resource with POST should usually return 201 Created, often with a Location header pointing to the new resource. A successful DELETE with nothing to return is commonly 204 No Content. Asynchronous operations that are accepted but not finished may return 202 Accepted.
Error codes deserve the same care. A missing or invalid token should give 401 Unauthorized (despite the name, it means “not authenticated”). A valid user trying to do something they are not allowed to do should give 403 Forbidden. Many APIs deliberately return 404 Not Found when a user asks for someone else’s record, so as not to reveal that it exists; whichever choice the API makes should be consistent. Malformed input should give a 4xx code such as 400 or 422, never a 500. A 500 in response to bad input means validation is missing and an exception escaped.
Layer 2: headers
Headers are easy to forget because tools hide them by default. Worth checking:
- Content-Type matches the body, for example
application/jsonwith the expected charset. Some clients fail if this is wrong even when the body is fine. - Caching headers on responses containing personal data, which should normally prevent shared caches from storing them.
- Security headers your organisation requires, such as Strict-Transport-Security.
- Pagination and rate-limit headers, if the API uses them, such as a total count or remaining-requests value.
- Absence of leaks, such as headers revealing server software versions or internal hostnames.
Layer 3: structure, checked with a schema
Before checking any particular value, it is efficient to check the shape of the whole body against a schema. JSON Schema is the common standard for this. A schema for a user response might state that the body is an object; that id is an integer, name is a non-empty string, email is a string in email format, and status is one of “ACTIVE”, “SUSPENDED” or “CLOSED”; that id, name and status are required; and that addresses is an array of objects with their own rules.
Validating against this in one step catches a whole family of defects: a field renamed, a number sent as a string, a required field that is now sometimes null, an enum value that nobody told the mobile team about. Most API tools support it, including Postman’s test scripts, RestAssured’s JSON schema validator module, and the jsonschema library in Python. If the team maintains an OpenAPI specification, the schemas inside it can be reused so that tests and documentation cannot drift apart.
Two settings change how strict the check is. Marking fields as required is what catches disappearing fields; without it, a schema check passes on an empty object. Disallowing additional properties catches unexpected new fields, which can reveal accidental data exposure, such as an internal passwordHash field suddenly appearing in the response. Whether to disallow extras is a design choice: it is strict for security but may break tests every time a field is legitimately added.
Also note that JSON Schema has several drafts (draft-04, draft-07, 2019-09 and 2020-12 are all in use), and validators differ in which they support. A schema written for one draft can behave differently under another, so pin the version.
Layer 4: values that mean something
A schema says the price is a number; only a value check says it is the right number. Value checks should be tied to the test’s own setup. If the test created a user called Asha with a specific email, it should check that fetching that user returns exactly those values, not just “some string”. For calculated values such as order totals or tax, compare with an independently computed expectation.
Look for consistency inside the body as well: line items adding up to the total, a count field matching the number of returned items, dates in the expected time zone (an API that silently returns UTC instead of IST will shift orders placed near midnight onto the wrong day).
Layer 5: what happened behind the response
A response describes what the server claims it did. For operations that change data, verify the effect separately. After a POST that creates an order, fetch the order through a GET, or query the database, and confirm it exists with the right values. Check that related effects occurred: stock reduced, an event published to the queue, a notification triggered. For a DELETE, confirm the resource is really gone, or soft-deleted according to the design.
Equally important is what should not happen. A failed payment should not leave a half-created order behind. A rejected request should not change anything. Repeating an idempotent request such as PUT or DELETE should leave the system in the same state as sending it once.
Layer 6: non-functional basics
A functional API test can cheaply record response time and flag anything wildly outside expectations, though proper performance testing needs dedicated load tools. It can also check that error messages are helpful to clients without exposing stack traces, SQL fragments or internal class names, which are useful to attackers.
None of these layers needs to be checked in every single test. A sensible suite runs schema validation broadly, value checks in focused tests, and side-effect checks for the operations that matter most. The point is to choose consciously, rather than letting a green 200 stand in for evidence that the API works.
Written by the RP Strativa Team. The field names and values in the examples are illustrative.
Leave a Reply