Логотип Workflow

Article

Http Response Statuses

Stage 4. Response Status Codes

An HTTP status code is the first result signal a client receives, even before parsing the response body. In API systems this is critical: mobile apps, frontend clients, retry logic, monitoring, and alerts all react to that code. Because of that, status codes are part of the API contract, not a cosmetic detail.

Status codes map

1. The Core Grouping Model

For practical work, three groups explain most real cases:

  • 2xx: request was processed successfully.
  • 4xx: problem in client request, access rights, or request context.
  • 5xx: server-side failure.

This simple model already improves troubleshooting. If 4xx rates increase, investigate validation, permissions, and client flows. If 5xx rises, inspect backend stability and downstream dependencies.

2. Minimal Status Set You Should Master First

CodeWhen to use itWhat the client should understand
200 OKSuccessful read or update with response bodyOperation succeeded; body data can be consumed
201 CreatedNew resource created successfullyResource exists now, often with id and/or Location
204 No ContentSuccess with no response payload neededOperation succeeded; no body parsing required
400 Bad RequestRequest format is invalidRequest must be technically fixed
401 UnauthorizedMissing or invalid authenticationRe-authentication or token refresh is needed
403 ForbiddenAuthenticated user lacks required permissionLogin retry will not solve it; access rights are needed
404 Not FoundResource does not exist at requested pathTarget object is unavailable or absent
409 ConflictRequest conflicts with current resource stateOperation is logically blocked by current state
422 Unprocessable EntityJSON is syntactically valid but breaks business rulesField values must be corrected according to domain rules
500 Internal Server ErrorUnexpected server failureProblem is on backend side

For beginner-to-intermediate API development, this set is enough. There is no need to memorize many rare codes before facing a specific use case.

3. High-Value Distinctions Developers Often Mix Up

401 versus 403 is the most common confusion. 401 means authentication is missing, expired, or invalid. 403 means the user is authenticated but not allowed to perform the action. This directly affects UX: the first case should drive login/token refresh, the second should show an access-denied flow.

400 versus 422 is another important distinction. 400 means the request is technically malformed, such as broken JSON syntax or wrong structural shape. 422 means syntax is valid, but business validation failed, for example weak password or invalid date range.

4. Status Code and Error Body Must Reinforce Each Other

Status code gives the error class; response body gives the details needed by humans and machines.

{
  "error": "validation_failed",
  "message": "password is too weak",
  "fields": {
    "password": "min 12 chars, at least one digit"
  },
  "traceId": "c92ab1d4"
}

If this error schema is consistent across endpoints, clients can implement one reusable handler instead of endpoint-specific exception branches.

5. Why “Always Return 200” Is Harmful

Some teams return 200 OK for every outcome and place failure flags such as success=false in the body. This breaks protocol semantics: monitoring counts failures as success, retry behavior becomes unreliable, and clients must implement custom parsing logic instead of standard HTTP handling. In production, this increases complexity and maintenance cost.

6. Practical Scenario: User Registration Endpoint

Consider a single endpoint: POST /users.

  1. Valid request, user created: return 201 Created.
  2. Email already exists: return 409 Conflict.
  3. Email format invalid or password fails policy: return 422 Unprocessable Entity.
  4. JSON syntax is broken: return 400 Bad Request.
  5. Database is unavailable: return 500 Internal Server Error.

In this flow, each code maps to a different problem class, so client behavior is predictable: ask user to fix fields, show duplicate-email feedback, suggest retry later, or route to authentication recovery when needed.

7. Release Checklist for a New Endpoint

  1. Does each major outcome have a distinct and logical status code?
  2. Is the border between 400 and 422 clear?
  3. Are 401 and 403 used according to authentication vs authorization rules?
  4. Is error-body structure consistent across related endpoints?
  5. Do automated tests protect status behavior from accidental refactor regressions?

Please login to pass quizzes.

Quiz

Check what you learned

Practice

Interactive practice

Complete tasks and check your answer instantly.