Tell us about HTTP status codes.
sobes.tech AI
Answer from AI
HTTP status codes are three-digit numbers returned by the server in response to a client's request. They indicate the result of the request and provide information on how the client should handle the response.
Status codes are divided into five classes:
- 1xx — Informational: The request has been received, and the process is continuing.
- 2xx — Success: The request was successfully received, understood, and accepted.
- 3xx — Redirection: Further action is needed to complete the request.
- 4xx — Client Error: The request contains bad syntax or cannot be fulfilled.
- 5xx — Server Error: The server failed to fulfill an apparently valid request.
Some of the most common status codes:
| Code | Description | Class |
|---|---|---|
| 200 | OK | Success |
| 201 | Created | Success |
| 204 | No Content | Success |
| 301 | Moved Permanently | Redirection |
| 302 | Found | Redirection |
| 400 | Bad Request | Client Error |
| 401 | Unauthorized | Client Error |
| 403 | Forbidden | Client Error |
| 404 | Not Found | Client Error |
| 500 | Internal Server Error | Server Error |
| 503 | Service Unavailable | Server Error |
When testing APIs, it is important to check for correct status codes in response to various requests (successful, validation errors, incorrect data, etc.) to ensure predictable system behavior.
For example, when creating a resource, 201 is expected; for successful data retrieval, 200; for a non-existent resource, 404; and for server errors, 500.
An example of checking the status code in an automated test (using Python with requests):
import requests
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)
# Check that the request returned status 200 OK
assert response.status_code == 200