QA Interview Hub · Knowledge base · Quizzes · Українська

API › Status codes

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

1xx wait, the request goes on 2xx the request is done 3xx go elsewhere or use cache 4xx the client's request is bad 5xx a failure on the server
Figure 1. 4xx and 5xx are highlighted: defects are searched there, but a 4xx can be the right reaction, while a 5xx almost never is.

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.

CodeMeaningWhen to expect
200 OKSuccess with a bodyGET, a successful PUT or PATCH
201 CreatedResource createdPOST, the Location header
202 AcceptedAccepted, processing goes onA long operation
204 No ContentSuccess without a bodyDELETE, PUT without a body
301 / 308Permanent redirectA changed URL (308 keeps the method)
302 / 307Temporary redirectA redirect to login (307 keeps the method)
304 Not ModifiedUnchanged, use the cacheA conditional GET with ETag
400 Bad RequestInvalid requestBroken JSON, missing parameter
401 / 403Not authenticated / no rightsToken, roles
404 Not FoundNo such resourceA non-existent id
405 Method Not AllowedMethod not supportedThe Allow header is present
409 ConflictState conflictA duplicate, a parallel change
410 GoneRemoved for goodA retired API version
412 / 428Condition failed / condition requiredIf-Match
415 Unsupported Media TypeUnsupported body formatA wrong Content-Type
422 Unprocessable ContentSyntax ok, content not acceptableA rule is broken
429 Too Many RequestsLimit exceededRate limiting, Retry-After
500 Internal Server ErrorUnhandled errorAlways a defect
502 / 503 / 504Gateway, unavailable, timeoutA 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.

In an interview. Junior: 2xx is success, 3xx is redirection, 4xx is a client error, 5xx is a server error. Creation gives 201, deletion 204, a missing resource 404. Middle/Senior: The class answers whose fault it is and whether a retry makes sense: 4xx must be fixed (except 408 and 429), 5xx can be retried. An unknown code is read as the 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.

More articles in this chapter