๐ Introduction to HTTP Status Codes
Every HTTP response returned by a web server contains a numeric status code. This code informs the client about the outcome of its request. Whether a request succeeds, fails, redirects, or encounters an unexpected problem, the status code communicates the result in a standardized way.
Understanding HTTP status codes is essential for web developers, API consumers, testers, and system administrators because they simplify debugging, monitoring, and error handling.
Information
๐ Status Code Categories
The first digit determines the overall meaning of the response. Individual codes within each category provide more specific information about what happened during request processing.
๐ Request Lifecycle
The client sends an HTTP request to the server.
The server validates, processes, and performs the requested operation.
The server returns an HTTP response containing headers, optional body, and a status code.
The client interprets the status code and decides the next action, such as displaying data, retrying, or showing an error message.
๐ Understanding Each Category
Informational responses indicate that the request has been received and processing continues. These codes are rarely encountered during everyday application development.
- 100 Continue
- 101 Switching Protocols
- 103 Early Hints
Success responses indicate that the request completed successfully and the server fulfilled the client's request.
- 200 OK
- 201 Created
- 202 Accepted
- 204 No Content
Redirection responses tell the client that the requested resource has moved or another action is required before completing the request.
- 301 Moved Permanently
- 302 Found
- 304 Not Modified
- 307 Temporary Redirect
Client Error responses indicate that the request contains invalid data or the client lacks permission to access the resource.
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 409 Conflict
- 429 Too Many Requests
Server Error responses indicate that the server failed to process a valid request due to an internal problem.
- 500 Internal Server Error
- 501 Not Implemented
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
๐ Common HTTP Status Codes
| Status Code | Meaning | Typical Usage |
|---|---|---|
| 200 | OK | Successful request |
| 201 | Created | New resource created |
| 204 | No Content | Success without response body |
| 301 | Moved Permanently | Permanent URL change |
| 304 | Not Modified | Use cached response |
| 400 | Bad Request | Invalid request |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | Permission denied |
| 404 | Not Found | Resource unavailable |
| 500 | Internal Server Error | Unexpected server failure |
| 503 | Service Unavailable | Server temporarily unavailable |
๐ป HTTP Response Example
A successful API request typically returns a 200 OK status code.
HTTP Response
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 1,
"name": "Alice"
}โ Example: Resource Not Found
404 Response
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"message": "User not found"
}๐งญ Choosing the Correct Status Code
๐งฎ Status Code Formula
The response category can be determined mathematically using integer division.
For example, if the status code is 404, then:
Therefore, 404 belongs to the 4xx Client Error category.
โ Best Practices
- Return 200 only when the request succeeds.
- Use 201 after creating a new resource.
- Return 204 when no response body is needed.
- Use 400 for malformed client requests.
- Return 401 for missing authentication and403 for insufficient permissions.
- Never return 200 for failed operations.
- Log all unexpected 5xx server errors for debugging.
Best Practice
๐ Learn More
The official HTTP Semantics specification provides complete definitions for every standardized status code.
Visit MDN HTTP Status Documentation or RFC 9110 HTTP Semantics for detailed reference material.