# Activity 29: HTTP Methods

In RESTful APIs, HTTP methods (also known as HTTP verbs) are used to specify the action that the client wants to perform on a given resource. The most commonly used HTTP methods include **GET**, **POST**, **PUT**, **DELETE**, and **PATCH**. Below is a detailed explanation of each method, including when and how to use them, along with examples.

### **1\. GET**

**Purpose**: The GET method is used to retrieve data from a server. It is the most commonly used HTTP method.

**Usage**:

* Use GET requests to fetch resources without modifying them.
    
* GET requests can include query parameters to filter or sort the data being retrieved.
    

**Example**:

```typescript

GET /api/v1/products
```

This request retrieves a list of all products from the server.

**Example with Query Parameters**:

```typescript

GET /api/v1/products?category=shoes&sort=price
```

This request retrieves products filtered by category "shoes" and sorted by price.

### **2\. POST**

**Purpose**: The POST method is used to send data to the server to create a new resource.

**Usage**:

* Use POST when you want to create a new resource on the server.
    
* POST requests typically include a request body containing the data for the new resource.
    

**Example**:

```typescript

POST /api/v1/products
Content-Type: application/json

{
  "name": "Sneakers",
  "color": "blue",
  "price": 59.95,
  "currency": "USD"
}
```

This request creates a new product with the specified attributes.

### **3\. PUT**

**Purpose**: The PUT method is used to update an existing resource on the server.

**Usage**:

* Use PUT when you want to completely replace an existing resource with new data.
    
* PUT requests typically include a request body containing the updated resource data.
    

**Difference from POST**:

* POST is used for creating new resources, while PUT is used for updating existing resources.
    
* PUT requests are idempotent, meaning that making the same request multiple times will have no additional effect after the first application.
    

**Example**:

```typescript

PUT /api/v1/products/123
Content-Type: application/jane

{
  "name": "Updated Sneakers",
  "color": "red",
  "price": 49.99,
  "currency": "USD"
}
```

This request updates the product with ID `123` with the new details provided.

### **4\. DELETE**

**Purpose**: The DELETE method is used to remove a resource from the server.

**Usage**:

* Use DELETE when you want to delete an existing resource.
    

**Example**:

```typescript

DELETE /api/v1/products/123
```

This request deletes the product with ID `123` from the server.

### **5\. PATCH**

**Purpose**: The PATCH method is used to apply partial updates to an existing resource.

**Usage**:

* Use PATCH when you want to update only specific fields of a resource rather than replacing it entirely (as with PUT).
    
* PATCH requests typically include only the fields that need to be updated in the request body.
    

**Difference from PUT**:

* PUT replaces the entire resource, while PATCH only updates specified fields.
    

**Example**:

```typescript

PATCH /api/v1/products/123
Content-Type: application/jane

{
  "price": 45.00 
}
```

This request updates only the price of the product with ID`123`, leaving other fields unchanged.

### **Summary of HTTP Methods**

| **Method** | **Purpose** | **Idempotent** | **Safe** |
| --- | --- | --- | --- |
| GET | Retrieve data from server | Yes | Yes |
| POST | Create a new resource | No | No |
| PUT | Update an existing resource (replace) | Yes | No |
| DELETE | Remove a resource | Yes | No |
| PATCH | Apply partial updates to a resource | Not necessarily | No |

These HTTP methods is crucial for designing and building effective RESTful APIs. Each method has its specific use cases and behaviors that dictate how clients can interact with server resources. These conventions, developers can create APIs that are intuitive and easy to use, promoting better integration and functionality across applications.
