Junior
Tell me about HTTP status codes.
sobes.tech AI
Answer from AI
HTTP status codes are three-digit integers returned by the server in response to a client's request. They indicate the result of the request and are divided into five classes:
- 1xx Informational: Request received, processing continues.
100 Continue: The initial part of the request has been received, and the client can continue sending the rest.101 Switching Protocols: The server understands and is willing to comply with the request to switch protocols.
- 2xx Success: The request was successfully received, understood, and accepted.
200 OK: Standard response for successful HTTP requests.201 Created: The request has been fulfilled and resulted in a new resource being created.204 No Content: The server successfully processed the request, but no content is returned.
- 3xx Redirection: Further action is needed to complete the request.
301 Moved Permanently: The resource has a new permanent URI.302 Found: The resource is temporarily under a different URI.304 Not Modified: The resource has not changed since the last request.
- 4xx Client Error: The request contains bad syntax or cannot be fulfilled.
400 Bad Request: The server could not understand the request due to invalid syntax.401 Unauthorized: Authentication is required.403 Forbidden: The server understood the request, but refuses to authorize it.404 Not Found: The requested resource was not found.
- 5xx Server Error: The server failed to fulfill an apparently valid request.
500 Internal Server Error: A generic error message.502 Bad Gateway: The server, while acting as a gateway, received an invalid response.503 Service Unavailable: The server is not ready to handle the request.
Examples in testing:
- API testing: Check if the returned status codes match expectations (e.g.,
200 OKfor success,404 Not Foundfor missing resource). - Web application testing: Check redirects (
3xx), user input errors (4xx), service availability (503).
Example in Python with requests:
import requests
url = "https://www.example.com"
response = requests.get(url)
# Check if request was successful (status code 2xx)
if response.status_code == 200:
print("Request successful.")
else:
print(f"Error: Status code {response.status_code}")