Activity 30: HTTP Status Codes

In RESTful APIs, HTTP status codes are essential for conveying the outcome of client requests. They are categorized into five classes, each indicating a specific type of response:

1xx (Informational): These codes indicate that the request was received and is being processed, but there is no final response yet.

  • 100 Continue: The server has received the request headers and the client should proceed to send the request body.

  • 101 Switching Protocols: The server is switching protocols as requested by the client.

Example: A client sends a request with a Expect: 100-continue header. The server responds with a 100 Continue status, indicating that the client can proceed with sending the request body.

2xx (Success): These codes indicate that the client's request was successfully received, understood, and accepted.

  • 200 OK: The request was successful, and the server returned the requested data.

    Example: A client requests user information, and the server responds with a 200 OK status along with the user data.

  • 201 Created: The request was successful, and a new resource was created as a result.

    Example: A client submits data to create a new user, and the server responds with a 201 Created status, indicating that the user has been successfully created.

  • 204 No Content: The request was successful, but there is no content to send in the response.

    Example: A client sends a request to delete a resource, and the server responds with a 204 No Content status, indicating that the resource was successfully deleted and there is no additional content to return.

3xx (Redirection): These codes indicate that the client must take additional actions to complete the request.

  • 301 Moved Permanently: The requested resource has been permanently moved to a new URI.

    Example: A client requests a resource that has been permanently moved to a new location, and the server responds with a 301 Moved Permanently status, providing the new URI.

  • 302 Found: The requested resource resides temporarily under a different URI.

    Example: A client requests a resource that is temporarily located elsewhere, and the server responds with a 302 Found status, indicating the temporary URI.

4xx (Client Error): These codes indicate that the client seems to have erred.

  • 400 Bad Request: The request could not be understood by the server due to malformed syntax.

    Example: A client sends a request with invalid syntax, and the server responds with a 400 Bad Request status, indicating that the request could not be processed.

  • 401 Unauthorized: Authentication is required and has failed or has not yet been provided.

    Example: A client attempts to access a protected resource without proper authentication, and the server responds with a 401 Unauthorized status, prompting the client to provide valid credentials.

  • 403 Forbidden: The client does not have permission to access the resource.

    Example: A client attempts to access a resource they are not authorized to view, and the server responds with a 403 Forbidden status, indicating that access is denied.

  • 404 Not Found: The requested resource could not be found but may be available in the future.

    Example: A client requests a resource that does not exist on the server, and the server responds with a 404 Not Found status, indicating that the resource could not be located.

5xx (Server Error): These codes indicate that the server failed to fulfill a valid request.

  • 500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.

    Example: A client sends a valid request, but the server encounters an unexpected error while processing it, and responds with a 500 Internal Server Error status.

  • 503 Service Unavailable: The server is currently unable to handle the request due to temporary overloading or maintenance.

    Example: A client sends a request during server maintenance, and the server responds with a 503 Service Unavailable status, indicating that the service is temporarily unavailable.

Understanding these status codes is crucial for both clients and servers to effectively communicate the outcome of API requests.

0
Subscribe to my newsletter

Read articles from John Carlo Regala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

John Carlo Regala
John Carlo Regala