# Activity#30: HTTP Status Codes

**HTTP Status Codes in RESTful APIs:** A Comprehensive **Guide**

HTTP status codes are essential for RESTful APIs, providing a standardized way for servers to communicate the outcome of a client's request. These codes are three-digit numbers, categorized into five classes based on the first digit.

**1xx (Informational)** These codes indicate that the request has been received and is being processed, but the server has not yet completed the request.

**Example: 100 Continue:** The server has received the request headers and is asking the client to send the request body. This is often used in POST requests to prevent the client from sending a large body if the request is going to be rejected.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838413035/1a5cdd50-b56b-43b5-9f47-318584e9f0c8.png align="center")

### **2xx (Success)**

These codes indicate that the request was successful and the server has completed the request.

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

**Example: GET /users/123:** This request successfully retrieves the user with ID 123.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838491387/3800e6c6-0a03-455d-b31d-4c95b9b716c1.png align="center")

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

**Example: POST /users:** This request successfully creates a new user.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838554936/4a06b8de-6e7b-4c97-b2c9-53fa8c2125cc.png align="center")

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

**Example: DELETE /users/123:** This request successfully deletes the user with ID 123.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838607403/293f922d-7c61-4920-95d5-ce7920b1e6a6.png align="center")

### **3xx (Redirection)**

These codes indicate that the client must take additional actions to complete the request. This usually involves redirecting the client to a different URL.

**Example: 301 Moved Permanently:** The requested resource has been permanently moved to a new URL. The client should update its records to use the new URL.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838674982/b1bd947c-69e7-4c1a-9e61-2cb455d9f722.png align="center")

### **4xx (Client Error)**

These codes indicate that the client has made an error in its request.

**400 Bad Request:** The request was invalid or cannot be understood by the server.

**Example: POST /users:** This request is missing required fields or has invalid data.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838719876/16af5e5d-1eb9-4368-8cf4-043d34b7e754.png align="center")

**401 Unauthorized:** Authentication is required for the requested resource.

**Example: GET /admin/dashboard:** This request requires authentication, but the client has not provided valid credentials.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838757892/e4b2c876-68b6-4591-a03c-5c2d9340a45b.png align="center")

**403 Forbidden:** The client does not have permission to access the resource, even if authenticated.

**Example: GET /admin/users:** This request requires admin privileges, but the client only has user privileges.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838798274/163afd7b-e385-4f53-b6e7-de0ab0b1d51f.png align="center")

**404 Not Found:** The requested resource could not be found.

**Example: GET /users/999:** There is no user with ID 999.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838880093/c37f0b68-4553-44e5-a449-58739aa792e3.png align="center")

### **5xx (Server Error)**

These codes indicate that the server encountered an error and could not complete the request.

**500 Internal Server Error:** The server encountered an error and could not complete the request.

**Example:** The server might have a bug, or a database connection might be lost.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728838952384/6d183ef8-4f6e-4037-bed7-8d9e62954aac.png align="center")

**503 Service Unavailable:** The server is temporarily unavailable, often due to maintenance or overload.

**Example:** The server is undergoing scheduled maintenance, or it is experiencing a high volume of traffic.

**Example Response:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728839025067/9e0e9b68-9fdc-4cf2-b2ca-7f81ef95ae9e.png align="center")

### **Key Takeaways:**

HTTP status codes are essential for RESTful API communication.

They provide clear information about the outcome of a request.

Clients should handle status codes appropriately, providing meaningful feedback to users.

Proper use of status codes improves API reliability and user experience.

  

By understanding and implementing these HTTP status codes, you can build robust and user-friendly RESTful APIs.
