# Activity 30: HTTP Status Codes

* **1xx (Informational):** We used the 1xx HTTP Status code to indicate and ongoing process of the current request, kind of like reassuring that the request is being processed by the back-end and awaiting the result of the request. Use case includes but is not limited to upgrading from HTTP to Web Socket when the client is sending a large POST request and needs to know whether the server is willing to accept it.
    
    * ```makefile
        HTTP/1.1 100 Continue
        ```
        
* **2xx (Success):** The most typical status code, HTTP 200 OK, indicates that the server has successfully processed the request.
    
    * **201**: The request succeeded, and a new resource was created as a result. This is typically the response sent after POST requests or some PUT requests. Use case of this includes, but no limited to: creating a user account that satisfies business logic, creating a post for a blogging platform for instance.
        
    * **200**: The request was granted. The HTTP method determines the outcome and meaning of "success":
        
        * [`GET`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET): The message body contains the resource that was retrieved and sent. This is use for instance when fetching posts that is tied in a certain `user_id`
            
        * [`HEAD`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD): The answer contains representation headers but no message body.
            
        * [`PUT`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) or [`POST`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST): The message body contains the resource that describes the action's outcome.
            
    * **204**: The headers are helpful, but there is nothing to send with this request. The user agent may add the updated headers to its cache for this resource.
        
        ```makefile
        HTTP/1.1 200 OK
        Content-Type: application/json
        Content-Length: 85
        
        {
          "status": "success",
          "data": {
            "id": 12345,
            "name": "John Doe",
            "email": "johndoe@example.com"
          }
        }
        
        HTTP/1.1 201 Created
        Location: /users/12345
        Content-Type: application/json
        Content-Length: 97
        
        {
          "status": "success",
          "data": {
            "id": 12345,
            "name": "Jane Doe",
            "email": "janedoe@example.com"
          }
        }
        
        HTTP/1.1 204 No Content
        ```
        
* **3xx (Redirection):** Redirecting traffic is indicated by HTTP **3xx** status codes. They indicate that more steps need to be taken by the client to fulfill the request. Usually, these codes inform the client that a resource has been relocated, migrated, or requires a different method of access.
    
    ```makefile
    HTTP/1.1 301 Moved Permanently
    Location: https://newsite.example.com
    Content-Length: 0
    
    HTTP/1.1 302 Found
    Location: https://temporary.example.com
    Content-Length: 0
    
    HTTP/1.1 303 See Other
    Location: https://example.com/results/12345
    Content-Length: 0
    ```
    
* **4xx (Client Error):** Client-side problems are indicated by HTTP **4xx** status codes, which indicate that a problem on the client's end prevents the server from processing the request. A distinct kind of client error is indicated by each code in the 4xx category.
    
    * **400:** The most typical status code, HTTP 200 OK, indicates that the server has successfully processed the request.
        
    * **401 Unauthorized**: When a request for user authentication receives a 401 Unauthorized answer, it indicates that either no credentials were supplied or the credentials are invalid.
        
    * **403 Forbidden**: The server understands the request but declines to approve it, as shown by a 403 Forbidden response. The client is authorized (or does not require authentication), yet they are nevertheless denied access to the resource, in contrast to a 401 status.
        
    * **404 Not Found**: The server was unable to locate the resource that the client had requested, as shown by the 404 Not Found response code. This may occur when there is an error in the URL, the resource has been deleted, or it was never created.
        
        ```makefile
        HTTP/1.1 400 Bad Request
        Content-Type: application/json
        Content-Length: 60
        
        {
          "error": "Bad Request",
          "message": "Invalid input data"
        }
        
        
        HTTP/1.1 401 Unauthorized
        WWW-Authenticate: Bearer realm="example"
        Content-Type: application/json
        Content-Length: 77
        
        {
          "error": "Unauthorized",
          "message": "Authentication credentials are missing or invalid"
        }
        
        
        HTTP/1.1 403 Forbidden
        Content-Type: application/json
        Content-Length: 68
        
        {
          "error": "Forbidden",
          "message": "You do not have permission to access this resource"
        }
        
        
        HTTP/1.1 404 Not Found
        Content-Type: application/json
        Content-Length: 51
        
        {
          "error": "Not Found",
          "message": "Resource not found"
        }
        ```
        
* **5xx (Server Error):** Server-side problems are indicated by HTTP 5xx status codes, which indicate that the server ran into an issue that made it impossible for it to complete the request. These failures imply that although the client's request was legitimate, internal problems prevented the server from responding appropriately.
    
    * **500:** The generic error message 500 Internal Server Error indicates that the server was unable to complete the request due to an unforeseen circumstance. When the server is unable to identify a more precise fault, it responds with a catch-all message.
        
    * **503:** A 503 Service Unavailable status code indicates that the server is presently unable to process the request, typically as a result of maintenance or temporary overloading. A 503 status, as opposed to a 500, indicates that the problem is **transient** and **ought** to be fixed shortly.
        
        ```makefile
        HTTP/1.1 500 Internal Server Error
        Content-Type: application/json
        Content-Length: 70
        
        {
          "error": "Internal Server Error",
          "message": "An unexpected error occurred"
        }
        
        
        HTTP/1.1 503 Service Unavailable
        Retry-After: 3600
        Content-Type: application/json
        Content-Length: 68
        
        {
          "error": "Service Unavailable",
          "message": "Server is temporarily overloaded"
        }
        ```
