HTTP status codes are the language your server speaks to clients. Every API response, web page load, and redirect is communicated through these three-digit codes. Understanding them is essential for building robust web applications, debugging API issues, and configuring proper error handling.
| Range | Category | Meaning |
|---|---|---|
1xx | Informational | Request received, continuing process |
2xx | Success | Request was received, understood, and accepted |
3xx | Redirection | Further action needed to complete the request |
4xx | Client Error | Request contains bad syntax or cannot be fulfilled |
5xx | Server Error | Server failed to fulfill a valid request |
| Code | Name | Description |
|---|---|---|
100 | Continue | Server received request headers; client should send the body. |
101 | Switching Protocols | Server is switching to a different protocol (e.g., WebSocket upgrade). |
103 | Early Hints | Server sends preliminary headers before the final response (for preloading). |
| Code | Name | When to Use |
|---|---|---|
200 | OK | Standard success response. GET returns resource, POST processes successfully. |
201 | Created | New resource was successfully created. Return after POST/PUT that creates something. |
202 | Accepted | Request accepted for processing but not completed. Used for async operations. |
204 | No Content | Success but no response body. Common for DELETE operations or PUT updates. |
206 | Partial Content | Returning a portion of the resource (range requests for large files/video). |
// POST /api/users → 201 Created (with Location header)
HTTP/1.1 201 Created
Location: /api/users/42
Content-Type: application/json
{"id": 42, "name": "Alice", "email": "[email protected]"}
// DELETE /api/users/42 → 204 No Content
HTTP/1.1 204 No Content
// PUT /api/users/42 → 200 OK (with updated resource)
HTTP/1.1 200 OK
Content-Type: application/json
{"id": 42, "name": "Alice Updated"}
| Code | Name | When to Use |
|---|---|---|
301 | Moved Permanently | Resource permanently moved. Search engines transfer SEO value. Use for domain changes. |
302 | Found (Temporary) | Temporary redirect. Original URL should still be used for future requests. |
303 | See Other | Redirect to a different resource after POST (Post/Redirect/Get pattern). |
304 | Not Modified | Cached version is still valid. No body sent; use your local cache. |
307 | Temporary Redirect | Like 302 but preserves the HTTP method. POST stays POST. |
308 | Permanent Redirect | Like 301 but preserves the HTTP method. POST stays POST. |
| Code | Name | When to Use |
|---|---|---|
400 | Bad Request | Invalid syntax, missing required fields, malformed JSON. |
401 | Unauthorized | Authentication required. JWT token missing or expired. |
403 | Forbidden | Authenticated but not authorized. The user does not have permission. |
404 | Not Found | Resource does not exist at this URL. |
405 | Method Not Allowed | HTTP method not supported (e.g., POST to a read-only endpoint). |
408 | Request Timeout | Server timed out waiting for the client's request. |
409 | Conflict | Request conflicts with current state (e.g., duplicate UUID, version conflict). |
413 | Payload Too Large | Request body exceeds the server's size limit. |
415 | Unsupported Media Type | Server does not accept the Content-Type sent. |
422 | Unprocessable Entity | Syntactically valid but semantically wrong (validation errors). |
429 | Too Many Requests | Rate limit exceeded. Include Retry-After header. |
// 401 Unauthorized - "Who are you?"
// The client has not provided authentication credentials.
// Fix: Include a valid Bearer token or API key.
GET /api/admin
Authorization: (missing)
→ 401 Unauthorized
// 403 Forbidden - "I know who you are, but you can't do this."
// The client is authenticated but lacks permission.
GET /api/admin
Authorization: Bearer valid-user-token
→ 403 Forbidden (user is not an admin)
| Code | Name | When to Use |
|---|---|---|
500 | Internal Server Error | Generic server failure. Unhandled exception, bug, or crash. |
501 | Not Implemented | Server does not support the requested functionality. |
502 | Bad Gateway | Reverse proxy (Nginx, Caddy) got an invalid response from upstream. |
503 | Service Unavailable | Server is temporarily overloaded or in maintenance. Include Retry-After. |
504 | Gateway Timeout | Upstream server did not respond in time. |
# Common causes of 502 Bad Gateway:
# 1. Backend application crashed or is not running
# 2. Wrong upstream address in reverse proxy config
# 3. Backend returned malformed response
# Check if the backend is running
curl http://localhost:8000/api/v1/health
# Check Nginx/Caddy error logs
tail -f /var/log/nginx/error.log
# Common causes of 504 Gateway Timeout:
# 1. Backend is too slow (database query, external API)
# 2. Proxy timeout too short
# Increase proxy timeout:
# nginx: proxy_read_timeout 120s;
# caddy: transport http { read_timeout 120s }
// Well-designed REST API responses:
// List resources
GET /api/tools → 200 OK
// Create resource
POST /api/tools → 201 Created
// Update resource
PUT /api/tools/1 → 200 OK (or 204 No Content)
// Delete resource
DELETE /api/tools/1 → 204 No Content
// Validation error
POST /api/tools (invalid data) → 422 Unprocessable Entity
{
"errors": [
{"field": "name", "message": "Name is required"},
{"field": "url", "message": "Invalid URL format"}
]
}
// Rate limited
GET /api/tools → 429 Too Many Requests
Retry-After: 60
Use 200 if you return the updated resource in the response body. Use 204 if there is no response body. The choice depends on whether clients need the updated data back immediately.
302 may change the HTTP method to GET on redirect (browsers do this). 307 guarantees the method is preserved. If you are redirecting a POST request and want it to remain a POST, use 307.
400 means the request is syntactically malformed (unparseable JSON, missing Content-Type). 422 means the syntax is valid but the data fails business validation (invalid email format, negative quantity). Many APIs use 400 for both, but 422 is more precise.
Return 200 when the service is healthy and 503 when it is not ready to accept traffic (e.g., database connection lost, warming up).
DevToolKit — 42+ Free Online Developer Tools