Understanding REST API Response Status Codes
When building or consuming RESTful APIs, understanding HTTP response status codes is essential. These codes tell the client whether a request was successful, failed, or needs additional action. In this guide, we break down the most commonly used status codes in REST APIs with simple explanations and real-world usage examples.
⭐ What Are HTTP Status Codes?
HTTP status codes are standardized responses sent by a server when processing an API request.
They are grouped into five categories:
-
1xx – Informational
-
2xx – Successful
-
3xx – Redirection
-
4xx – Client Errors
-
5xx – Server Errors
For REST APIs, the 2xx, 4xx, and 5xx groups are most important.
✅ 1. Success Status Codes (2xx)
These indicate that the request was successfully received and processed.
200 OK
The most common success response.
Used when a request is successful and returns data.
Example:
Returning a list of products from /api/products.
201 Created
Used when a new resource is created successfully.
Example:
A new user is registered via a POST request.
202 Accepted
The request is accepted but will be processed later.
Best for:
Background jobs, queue processing, asynchronous tasks.
204 No Content
The request was successful, but no response body is needed.
Common use case:
Successful DELETE request.
❌ 2. Client Error Status Codes (4xx)
These codes indicate a problem from the client side.
400 Bad Request
The server cannot process the request due to invalid input.
Example:
Missing required fields in a form submission.
401 Unauthorized
Authentication failed or was not provided.
Example:
API requires a valid JWT token.
403 Forbidden
The client is authenticated but does not have permission.
Example:
User tries accessing admin-only endpoints.
404 Not Found
The requested resource does not exist.
Example:
Product not found by ID.
405 Method Not Allowed
The request method is not supported on this endpoint.
Example:
Sending a GET request to an endpoint that only accepts POST.
409 Conflict
The request conflicts with the current state of the resource.
Example:
Trying to register with an email that already exists.
422 Unprocessable Entity
Validation errors occurred.
Laravel uses this by default for validation failures.
⚠️ 3. Server Error Status Codes (5xx)
These indicate the server encountered an unexpected condition.
500 Internal Server Error
A generic server-side error.
Causes:
Unhandled exceptions, logic errors.
502 Bad Gateway
The server received an invalid response from an upstream source.
503 Service Unavailable
The server is temporarily down or overloaded.
Example:
Maintenance mode, high traffic.
504 Gateway Timeout
The server took too long to respond.
Example:
Slow external API calls.
🧠Quick Summary Table
| Code | Meaning | Common Use |
|---|---|---|
| 200 | OK | Successful GET/PUT/PATCH/DELETE |
| 201 | Created | New resource created |
| 202 | Accepted | Async processing |
| 204 | No Content | Successful DELETE |
| 400 | Bad Request | Invalid request format |
| 401 | Unauthorized | Missing/invalid token |
| 403 | Forbidden | No permission |
| 404 | Not Found | Resource missing |
| 405 | Method Not Allowed | Wrong HTTP method |
| 409 | Conflict | Duplicate or conflicting data |
| 422 | Validation Error | Laravel validation |
| 500 | Internal Server Error | Unexpected failure |
| 503 | Unavailable | Server down |
🎯 Conclusion
REST API status codes are essential for building clean, predictable, and user-friendly APIs.
Choosing the correct response code:
-
Makes debugging easier
-
Improves client-side logic
-
Enhances API documentation
-
Clarifies error handling for developers
By using these codes consistently, you create a more professional and maintainable API.
Comments
Post a Comment