Activity 30: HTTP Status Codes

1xx: Informational
These codes indicate that the request has been received, and the server is processing it, but there is no final response yet. These codes are rarely used in REST APIs.
100 Continue: The client can continue with the request. This is typically used when the client sends a large request body, and the server is verifying the headers before the full request body is sent.
101 Switching Protocols: The server is switching protocols as requested by the client (e.g., upgrading from HTTP/1.1 to HTTP/2).
Example:
{
"status": 100,
"message": "Data retrieved successfully."
}
2xx: Success
These codes indicate that the request was successfully received, understood, and processed by the server.
200 OK: The request was successful, and the server returned the requested data (for GET requests).
Example:
{ "status": 200, "data": { "id": 1, "name": "Christine Maitom" } }201 Created: The request was successful, and a new resource was created. This is often used for POST requests where new data is created.
Example:
{ "status": 201, "message": "Resource created successfully.", "data": { "id": 2, "name": "Christine Maitom" } }204 No Content: The request was successful, but there is no content to send in the response. This is often used for DELETE or PUT requests.
Example:
{ "status": 204, "message": "Request successful, but no content to return." }
3xx: Redirection
These codes indicate that further action is required to complete the request, usually in the form of a redirect to another URL.
301 Moved Permanently: The resource has been moved permanently to a new URL. The client should use the new URL for future requests.
Example:
{ "status": 301, "message": "The resource has been permanently moved to a new location.", "location": "https://new-url.com/resource" }302 Found: The resource has been temporarily moved to a new URL, and the client should continue to use the original URL for future requests.
Example:
{ "status": 302, "message": "Resource temporarily found at another location.", "location": "https://temporary-url.com/resource" }304 Not Modified: The resource has not been modified since the last request, so no content is returned.
4xx: Client Error
These codes indicate that the client has made an error in the request, which prevents it from being fulfilled.
400 Bad Request: The server cannot process the request because the client sent invalid data (e.g., missing required parameters).
Example:
{ "status": 400, "error": "Bad Request", "message": "The request was malformed or missing required fields." }401 Unauthorized: The client must authenticate to access the requested resource. This typically occurs if the client has not provided valid authentication credentials (e.g., a missing or incorrect API token).
Example:
{ "status": 401, "error": "Unauthorized", "message": "Authentication is required to access this resource." }403 Forbidden: The server understands the request but refuses to authorize it. This could be because the client does not have permission to access the resource.
Example:
{ "status": 403, "error": "Forbidden", "message": "You do not have permission to access this resource." }404 Not Found: The requested resource could not be found on the server. This is one of the most common status codes encountered when a resource is missing or the URL is incorrect.
Example:
{ "status": 404, "error": "Not Found", "message": "The requested resource could not be found." }
5xx: Server Error
These codes indicate that the server failed to fulfill a valid request due to an internal error or temporary issue.
500 Internal Server Error: A generic error occurred on the server. This usually indicates an unexpected condition that prevented the server from fulfilling the request.
Example:
{ "status": 500, "error": "Internal Server Error", "message": "Something went wrong on the server. Please try again later." }503 Service Unavailable: The server is temporarily unable to handle the request, often due to being overloaded or undergoing maintenance.
Example:
{ "status": 503, "error": "Service Unavailable", "message": "The server is temporarily unavailable. Please try again later." }
Summary
These HTTP status codes serve as a standardized way to communicate the result of an API request. The main categories—informational (1xx), success (2xx), redirection (3xx), client error (4xx), and server error (5xx)—help both clients and servers understand the outcome and whether further actions are required.