API Status Codes
What Are Status Codes?
HTTP status codes are three-digit numbers returned by a server indicating the outcome of a request, grouped into categories by their first digit.
Common Categories
- 2xx (Success): e.g., 200 OK, 201 Created, 204 No Content.
- 3xx (Redirection): e.g., 301 Moved Permanently, 304 Not Modified.
- 4xx (Client Error): e.g., 400 Bad Request, 401 Unauthorized, 404 Not Found.
- 5xx (Server Error): e.g., 500 Internal Server Error, 503 Service Unavailable.
Why Testers Check Status Codes
The status code is often the very first check in an API test — confirming the server understood the request correctly before validating the body content in detail.
Codes That Testers Often Confuse
Knowing the category is not enough during testing. You need to judge whether the server picked the right code for the situation, because a wrong code is a real defect that client applications will trip over.
| Pair | Difference |
|---|---|
| 200 vs 201 vs 204 | 200 returns a result, 201 confirms a new resource was created, 204 means success with no body, common after DELETE |
| 400 vs 422 | 400 is a malformed request such as broken JSON; 422 means the syntax is fine but values fail validation. Many APIs use only 400, so check the API contract |
| 401 vs 403 | 401 means the caller is not authenticated or the token is invalid; 403 means the caller is known but not allowed |
| 404 vs 405 | 404 means the resource does not exist; 405 means the URL exists but the HTTP method is not supported there |
| 409 | Conflict with current state, for example registering an email that already exists |
| 429 | Too many requests, the client hit a rate limit |
| 502, 503, 504 | Bad response from an upstream server, service temporarily unavailable, and upstream timeout respectively |
There is also a 1xx group of informational responses, but you rarely assert on them in everyday API tests.
Designing Test Cases Around Status Codes
For each endpoint, plan at least one test per expected code. A user-creation endpoint might look like this:
- Valid body with a fresh email: expect 201 and a new id.
- Missing required field: expect 400 or 422 per the specification.
- No authorization header: expect 401.
- Token of a user without admin rights: expect 403.
- Same email sent twice: expect 409 on the second call.
- GET on an id that does not exist: expect 404.
- PATCH on an endpoint that only allows POST: expect 405.
Asserting Status Codes in Tools
Postman test script
pm.test("User is created", function () {
pm.response.to.have.status(201);
const body = pm.response.json();
pm.expect(body.id).to.exist;
pm.expect(body.email).to.eql(pm.variables.get("email"));
});
REST Assured in Java
given()
.header("Authorization", "Bearer " + token)
.contentType("application/json")
.body("{\"name\": \"Asha\", \"email\": \"asha@example.com\"}")
.when()
.post("/users")
.then()
.statusCode(201)
.body("id", notNullValue());
Where Status Code Testing Falls Short
- Errors hidden behind 200. Some APIs return 200 with a body like
{"success": false}. Raise this as a design issue, and always assert on the body too. - Accepting any 2xx. A check like "status below 300" lets a wrong 200 pass where 201 was required.
- Treating 5xx as expected. A 500 during a negative test means the server crashed on bad input. The correct response is a 4xx with a helpful message.
- Ignoring headers. A 201 should usually include a Location header, and a 429 often includes Retry-After.
- Leaking details. Error responses should not expose stack traces or database messages, which is both a usability and security problem.
Practical takeaway: write the expected status code next to every API test case before execution, taken from the API contract, not from what the server happens to return.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.