HTTP status codes every API tester should know
There are dozens of HTTP status codes. In API testing you'll see about fifteen of them over and over. Knowing what each one usually means in practice, not just its name, makes debugging a lot faster. This is that list, with the mix-ups that cost people the most time.
2xx: it worked
| Code | Name | What it usually means |
|---|---|---|
| 200 | OK | The request worked and there's a body |
| 201 | Created | A POST created something. Often with a Location header pointing at it |
| 202 | Accepted | Queued for later. The work isn't done yet, so don't assert on the result |
| 204 | No Content | Worked, and there's deliberately no body. Common for DELETE and some PUTs |
pm.test('creates the order', () => {
pm.expect(pm.response.code).to.equal(201);
});3xx: look elsewhere
| Code | Name | What it usually means |
|---|---|---|
| 301 / 308 | Moved Permanently / Permanent Redirect | Update the URL in your collection |
| 302 / 307 | Found / Temporary Redirect | Often a login redirect: you're not authenticated |
| 304 | Not Modified | Your cached copy is still good (you sent If-None-Match or If-Modified-Since) |
An API that answers a JSON request with a 302 to a login page is telling you your session or token isn't being accepted. Whether redirects are followed is a per-request setting in the request's Settings tab.
4xx: you sent something wrong
| Code | Name | What it usually means |
|---|---|---|
| 400 | Bad Request | Malformed JSON, a missing required field, a wrong type |
| 401 | Unauthorized | No credentials, or invalid ones. Despite the name, it's about authentication |
| 403 | Forbidden | Valid credentials, not allowed to do this |
| 404 | Not Found | Wrong path, or the resource doesn't exist. Some APIs also use it to hide resources you can't see |
| 405 | Method Not Allowed | The path exists, the method doesn't. POST where only GET works |
| 409 | Conflict | Duplicate, or the resource changed since you read it |
| 415 | Unsupported Media Type | Missing or wrong Content-Type, usually not application/json |
| 422 | Unprocessable Content | The JSON is valid but the values aren't: validation errors |
| 429 | Too Many Requests | Rate limited. Check the Retry-After header |
Test the 4xx responses on purpose. A good API test suite has as many tests for bad input as for good:
pm.test('rejects a negative quantity with a clear error', () => {
pm.expect(pm.response.code).to.equal(422);
pm.expect(pm.response.json().errors[0].field).to.equal('qty');
});5xx: the server failed
| Code | Name | What it usually means |
|---|---|---|
| 500 | Internal Server Error | A bug. Unhandled exception, nil pointer, failed query |
| 502 | Bad Gateway | A proxy or load balancer couldn't get a valid answer from the service behind it, often during a restart |
| 503 | Service Unavailable | Overloaded, in maintenance, or not ready yet |
| 504 | Gateway Timeout | The service behind the proxy took too long |
The distinction that matters for CI: 502, 503 and 504 are often transient: a deploy, a restart, a cold start. A 500 is almost always a real bug. That's why the Requestarium CLI's --retries option retries 502, 503 and 504 (and connection errors), but never a 500:
requestarium run-collection "Orders API" --env Staging --retries 2More in How to deal with flaky API tests.
A safety net for every request
Add one rule to a whole collection so no request can silently return a 5xx: in the collection's Tests tab, add Status code, less than, 500. It applies to every request inside, on top of their own checks. See API assertions without writing code.
And in the CLI's JUnit report, a request with no tests at all still gets a test case that checks its status is below 400, so nothing slips through.