๐ Introduction
When working with REST APIs, understanding HTTP status codes is essential for debugging, testing, and building reliable applications.
API error codes help clients and servers communicate the result of a request, whether it was successful, redirected, invalid, or failed due to server issues.
This article provides a complete and practical reference of common API error codes, categorized by type, with real-world scenarios to help developers, testers, and QA engineers quickly identify and fix issues.
✅ Success Codes (2xx)
These status codes indicate that the request was successfully received, understood, and processed by the server.
| Code | Meaning | When It Occurs |
|---|---|---|
| 200 | OK | Successful GET, PUT, PATCH, or DELETE request |
| 201 | Created | Resource successfully created using POST |
| 202 | Accepted | Request accepted for processing (asynchronous operations) |
| 204 | No Content | Successful request with no response body (commonly used for delete operations) |
๐ Redirection Codes (3xx)
Redirection codes indicate that the client must take additional action to complete the request, usually by accessing a different URL.
| Code | Meaning | When It Occurs |
|---|---|---|
| 301 | Moved Permanently | Resource permanently moved to a new URL |
| 302 | Found | Temporary redirection |
| 304 | Not Modified | Resource hasn’t changed (used for caching) |
| 307 | Temporary Redirect | Temporary redirect while preserving HTTP method |
| 308 | Permanent Redirect | Permanent redirect while preserving HTTP method |
❌ Client Error Codes (4xx)
These errors occur when the client sends an invalid request or lacks required permissions.
| Code | Meaning | When It Occurs |
|---|---|---|
| 400 | Bad Request | Invalid JSON, missing required fields, validation errors |
| 401 | Unauthorized | Missing or invalid authentication credentials |
| 403 | Forbidden | Authenticated but lacks permission |
| 404 | Not Found | Resource or endpoint does not exist |
| 405 | Method Not Allowed | Incorrect HTTP method used |
| 406 | Not Acceptable | Server cannot produce response as per Accept header |
| 408 | Request Timeout | Client request took too long |
| 409 | Conflict | Duplicate or conflicting resource |
| 410 | Gone | Resource permanently deleted |
| 411 | Length Required | Missing Content-Length header |
| 412 | Precondition Failed | Header conditions not met |
| 413 | Payload Too Large | Request body exceeds allowed size |
| 414 | URI Too Long | Request URL too long |
| 415 | Unsupported Media Type | Incorrect or missing Content-Type |
| 422 | Unprocessable Entity | Valid JSON but business rule validation failed |
| 423 | Locked | Resource is locked |
| 429 | Too Many Requests | Rate limit exceeded |
๐ฅ Server Error Codes (5xx)
These errors indicate that the server failed to process a valid request.
| Code | Meaning | When It Occurs |
|---|---|---|
| 500 | Internal Server Error | Unhandled exception or server crash |
| 501 | Not Implemented | Feature not supported by server |
| 502 | Bad Gateway | Invalid response from upstream server |
| 503 | Service Unavailable | Server down or under maintenance |
| 504 | Gateway Timeout | Upstream service timeout |
| 507 | Insufficient Storage | Server out of storage |
๐ค Common Confusion: 400 vs 415 vs 422
Understanding the difference between these three error codes avoids incorrect API handling.
| Code | Scenario | Example |
|---|---|---|
| 400 | Malformed request | Invalid JSON syntax, missing required fields |
| 415 | Wrong Content-Type | Sent text/plain instead of application/json |
| 422 | Business rule violation | age = -5 (valid format, invalid value) |
⚡ Quick Reference by Use Case
๐ Authentication & Authorization
401 → Not logged in
403 → Logged in but insufficient permission
๐ฆ Resource Handling
404 → Resource does not exist
410 → Resource permanently removed
๐งช Data Validation
400 → Syntax or malformed request
415 → Invalid content type
422 → Business validation error
๐ฆ Rate Limiting
429 → Too many requests
๐ฅ Server Issues
500 → Server error
503 → Server unavailable or under maintenance
๐ Conclusion
Understanding API error codes is critical for building robust applications, writing effective test cases, and troubleshooting production issues.
By correctly handling HTTP status codes, developers and testers can improve API reliability, user experience, and system stability.
Bookmark this guide as your quick reference for common API error codes.
Happy reading !!
No comments:
Post a Comment