# Activity 29: HTTP Methods

## **HTTP Methods**

RESTful APIs rely heavily on HTTP methods to define the actions performed on resources. These methods act as verbs, dictating how a client interacts with the server. Let's explore the core methods: GET, POST, PUT, DELETE, and PATCH, understanding their functionalities and use cases.

### **1\. GET: Retrieving Data from the Server**

The **GET** method is the cornerstone of retrieving data from a server. It's used to fetch a representation of a resource, without modifying the server's state. This makes GET requests **idempotent**, meaning multiple identical requests produce the same result.

**When to use GET:**

* **Retrieving a single resource:** Fetch a specific user profile by their ID: `/users/123`
    
* **Retrieving a collection of resources:** Get a list of all products: `/products`
    
* **Fetching data based on query parameters:** Retrieve products with a specific price range: `/products?price=10-50`
    

**Example:**

```css
GET /users/123 
```

This request would retrieve the data associated with the user with ID 123.

### **2\. POST: Creating New Resources**

The **POST** method is used to send data to the server to create a new resource. This method is **not idempotent**, meaning multiple identical requests will result in multiple resources being created.

**When to use POST:**

* **Creating a new user account:** `/users`
    
* **Adding a new product to an inventory:** `/products`
    
* **Submitting a form with user data:** `/forms`
    

**Example:**

```css
POST /products 
```

This request would send data in the request body to create a new product on the server.

### **3\. PUT: Replacing Existing Resources**

The **PUT** method is used to update an existing resource on the server. It replaces the entire resource with the data provided in the request body. This method is **idempotent**, meaning multiple identical requests will result in the same resource state.

**When to use PUT:**

* **Updating a user's profile information:** `/users/123`
    
* **Changing a product's details:** `/products/456`
    

```css
PUT /users/123
```

This request would replace the entire user profile with the data provided in the request body.

**Difference between PUT and POST:**

* **Idempotence:** PUT is idempotent, while POST is not.
    
* **Resource replacement:** PUT replaces the entire resource, while POST creates a new resource.
    
* **URL:** PUT uses the URL of the existing resource, while POST uses the URL of the resource collection.
    

### **4\. DELETE: Removing Resources**

The **DELETE** method is used to remove a resource from the server. It's **idempotent**, meaning multiple identical requests will result in the same resource state (deleted).

**When to use DELETE:**

* **Deleting a user account:** `/users/123`
    
* **Removing a product from the inventory:** `/products/456`
    

```css
DELETE /products/456
```

This request would delete the product with ID 456 from the server.

### **5\. PATCH: Applying Partial Updates**

The **PATCH** method is used to apply partial updates to a resource. It modifies only specific fields of the resource, unlike PUT which replaces the entire resource. PATCH is **not idempotent**, meaning multiple identical requests might result in different resource states.

**When to use PATCH:**

* **Updating a user's email address:** `/users/123`
    
* **Changing a product's price:** `/products/456`
    

**Example:**

```css
PATCH /users/123
```

This request would update only the email address of the user with ID 123, leaving other fields unchanged.

**Difference between PATCH and PUT:**

* **Partial updates:** PATCH applies partial updates, while PUT replaces the entire resource.
    
* **Idempotence:** PATCH is not idempotent, while PUT is.
    
* **Data in request body:** PATCH specifies the fields to be updated, while PUT sends the entire resource representation.
