Activity 30: HTTP Status Codes
HTTP Status Codes in RESTful APIs
RESTful APIs use HTTP status codes to communicate the outcome of requests between clients and servers. Here's a detailed look at the main categories:
1xx: Informational
These codes indicate that the request has been received and is being processed:
100 Continue: The server has received part of the request and asks the client to continue sending the rest.
102 Processing: The server is still processing the request.
2xx: Success
Indicates that the request was successfully processed:
200 OK: The request was successful, and the server returned the requested data. Example: A GET request for a user's profile returns a JSON response with the user details.
201 Created: A resource was successfully created. Example: A POST request to create a new user returns the created user's details.
204 No Content: The request was successful, but there is no data to return. Example: A DELETE request to remove a user returns this code when the operation succeeds without additional content.
3xx: Redirection
Indicates that further action is required to complete the request:
301 Moved Permanently: The requested resource has been moved to a new URL.
303 See Other: The client should make a GET request to a different URL to retrieve the desired resource.
4xx: Client Error
Indicates an error caused by the client:
400 Bad Request: The server cannot process the request due to invalid syntax. Example: Sending an improperly formatted JSON payload.
401 Unauthorized: Authentication is required. Example: Accessing a protected endpoint without valid credentials.
403 Forbidden: The client does not have permission to access the resource.
404 Not Found: The requested resource was not found on the server.
5xx: Server Error
Indicates issues on the server's side:
500 Internal Server Error: A generic error message indicating the server could not process the request.
503 Service Unavailable: The server is temporarily unable to handle the request, often due to maintenance or overload.
Examples of Responses
200 OK:
{
"id": 123,
"name": "John Doe",
"email": "johndoe@example.com"
}
404 Not Found:
{
"error": "Resource not found",
"status": 404
}
500 Internal Server Error:
{
"error": "Internal server error",
"status": 500
}
When and Why These Codes Are Used
These codes provide a standardized way to indicate the success or failure of a request, helping developers debug and ensuring consistency across API implementations.
REFERENCES:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
https://www.javaguides.net/2021/01/rest-api-http-status-codes.html