Groups and a table of codes
The first digit of a code sets the response class: 2xx success, 3xx redirection, 4xx client error, 5xx server error. A client reads an unknown code as the x00 of its class, so logic by class survives new codes. A 4xx usually means the request must change (408 and 429 are exceptions), a 5xx that the request may have been correct. To check a 301 or 307, a test disables redirects.
API · Status codes
The response is 304 with an empty body. A failure, or normal?
A test calls the catalogue a second time with If-None-Match, gets 304 with no body, and the code reads res.json() and fails. The developer says: "It all works, it is the cache." They are right, the mistake is in the test's expectations. To avoid memorising the table, first understand how the codes are grouped and what each group promises the client. Then the table is a hint, not a list to cram.
Five classes
Per RFC 9110 the first digit sets the response class. A client that does not know a specific code must treat it as the x00 of its class: an unknown 499 is read as 400, an unknown 599 as 500. So logic by class is robust against new codes.
Five classes and what they promise
Why it is built this way. The class answers two client questions: "did it work" and "whose fault is it". 4xx says that repeating the same request usually changes nothing until the client fixes it (the exceptions are 408 and 429, which are retried after a pause). 5xx says the request may have been fine, and a retry sometimes helps. That is enough for a client, a cache, a gateway and monitoring to decide without understanding the content.
| Code | Meaning | When to expect |
|---|---|---|
| 200 OK | Success with a body | GET, a successful PUT or PATCH |
| 201 Created | Resource created | POST, the Location header |
| 202 Accepted | Accepted, processing goes on | A long operation |
| 204 No Content | Success without a body | DELETE, PUT without a body |
| 301 / 308 | Permanent redirect | A changed URL (308 keeps the method) |
| 302 / 307 | Temporary redirect | A redirect to login (307 keeps the method) |
| 304 Not Modified | Unchanged, use the cache | A conditional GET with ETag |
| 400 Bad Request | Invalid request | Broken JSON, missing parameter |
| 401 / 403 | Not authenticated / no rights | Token, roles |
| 404 Not Found | No such resource | A non-existent id |
| 405 Method Not Allowed | Method not supported | The Allow header is present |
| 409 Conflict | State conflict | A duplicate, a parallel change |
| 410 Gone | Removed for good | A retired API version |
| 412 / 428 | Condition failed / condition required | If-Match |
| 415 Unsupported Media Type | Unsupported body format | A wrong Content-Type |
| 422 Unprocessable Content | Syntax ok, content not acceptable | A rule is broken |
| 429 Too Many Requests | Limit exceeded | Rate limiting, Retry-After |
| 500 Internal Server Error | Unhandled error | Always a defect |
| 502 / 503 / 504 | Gateway, unavailable, timeout | A crash, overload |
Example: two cases the client hides
Most HTTP clients follow redirects by default. A test calls an http:// address, sees 200 and does not notice that it is the result of a redirect, and of the wrong kind. The response:
HTTP/1.1 301 Moved Permanently
Location: https://api.shop.test/v1/orders/17
To check the redirect itself, it is switched off:
import { test, expect } from '@playwright/test';
test('http redirects to https', async ({ playwright }) => {
const api = await playwright.request.newContext();
const res = await api.get('http://api.shop.test/v1/orders/17', {
maxRedirects: 0, // otherwise the client follows and you see only 200
});
expect(res.status()).toBe(301);
expect(res.headers()['location']).toBe('https://api.shop.test/v1/orders/17');
await api.dispose();
});
Before: the test sees only the final 200. After: it checks 301, not 302, and the exact Location. The difference matters: a cache remembers 301 (and 308) for good, so a wrong permanent redirect breaks clients for a long time. One more subtlety: after 301 and 302 many clients change POST to GET, while 307 and 308 keep the method and the body. The second case, 304:
test('304 has no body', async ({ request }) => {
const first = await request.get('/v1/catalog');
const etag = first.headers()['etag'];
expect(etag).toBeTruthy();
const second = await request.get('/v1/catalog', {
headers: { 'If-None-Match': etag },
});
expect(second.status()).toBe(304);
expect(await second.text()).toBe('');
});
A 304 has no body, so reading res.json() makes no sense. The client uses its stored copy, and the test checks exactly that contract: 304 and an empty body.
Limits and alternatives
- 1xx is almost invisible in an API test: 100 Continue and 103 Early Hints are handled by the client or the server, and an HTTP library returns only the final response.
- The table describes typical matches. When the contract says otherwise (422 instead of 400, 404 instead of 403), the contract is right.
- Codes outside the table exist: 207 (WebDAV), 418 (a joke from RFC 2324). An unknown code is read by its class.
- In a browser the browser itself hides redirects and 304: JS sees the final response. These codes cannot be checked through the UI, only at the API level or in DevTools.
Common mistakes
- Expecting 200 everywhere. Creation is 201, deletion is 204, and a test for 200 misses an inexact code or fails on the right one.
- Reading JSON from 204 and 304. There is no body, and the test fails, not the API.
- Mixing up 301 and 302. A permanent redirect where a temporary one belongs gets cached and cannot be rolled back easily.
- Dismissing 3xx as "not an error". A redirect to a login page instead of 401 in an API is a defect: the client gets HTML where it expected JSON.
Check yourself
A GET test gets 200, but the developer says it should be a 302 to login. Why does the test not see it, and how to fix it?
Answer
The client followed the redirect and returned the final login page with 200. Switch redirects off (maxRedirects: 0 in Playwright) and check 302 and Location. For an API it is even better to expect 401, not a redirect to HTML.
A server returns code 499 (not from an RFC). How should a client read it?
Answer
As 400, a generic client error: an unknown code is handled as the x00 of its class. So logic by class is robust against new codes, and the details come from the error body.
x00 of its class. In tests I switch off automatic redirects to see 301 or 307, and I do not read JSON from 204 and 304. I check specific codes against the contract.