Skip to the page

API testing · Concept

Status codes for common API cases

The interviewer asks

Which status code should an API return for each of these: something created, a validation error, no token, a valid token but the wrong role, not found, a duplicate, too many requests, and a server crash? And what would you do if it returns 200 with an error message in the body?

Then

Follow-ups the interviewer asks next

  1. 401 or 403: what's the difference?
  2. 400 or 422 for a validation error?
  3. PUT or PATCH, and which methods are idempotent?
  4. What does 204 mean, and what's in its body?
  5. What would you check besides the status code?
  6. Sending a string where a number is expected gives a 500. Is that a bug?
  7. Which response headers do you look at?

The answer

What a strong answer covers

  • 201 Created (with a Location header or the new resource); 400 Bad Request or 422 Unprocessable Content for validation (a team choice, but consistent); 401 Unauthorized for missing or invalid credentials; 403 Forbidden for a known user without permission; 404 Not Found; 409 Conflict for duplicates; 429 Too Many Requests (with Retry-After); 500 for an unhandled error; 502, 503 and 504 for gateway and upstream problems.
  • 200 with {"error": ...} is a bug, or at least a contract problem: clients, monitoring and retries depend on status codes.
  • Idempotent: GET, PUT and DELETE give the same result if repeated; POST doesn't; PATCH not necessarily.
  • 204 No Content has no body.
  • Beyond the status: body values, the schema, headers (Content-Type, caching, security headers), response time, side effects (a database row, an event published), and error bodies that don't leak stack traces.
  • A wrong type giving 500 is a bug: input validation should answer with a 4xx.

The red pen

Common traps

Avoid:mixing up 401 and 403; "200 is fine because the message says error"; checking only the status code; not knowing 409 or 429.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview