# HTTP Methods and their use cases

HTTP methods define the types of actions that can be performed on resources in a RESTful API. Let's explore the five main HTTP methods and their specific use cases.

## 1\. GET Method

**Purpose**: Retrieve data from a specified resource

**Characteristics**:

* Safe (read-only)
    
* Idempotent (multiple identical requests have same effect)
    
* Cacheable
    
* Should not modify server state
    

```basic
GET /api/users                 # Get list of users
GET /api/users/123             # Get specific user
GET /api/posts?category=tech   # Get filtered posts
```

**Best Practices**:

1. Never use for data modification
    

## 2\. POST Method

**Purpose**: Create new resources or submit data for processing

**Characteristics**:

* Not safe (modifies data)
    
* Not idempotent
    
* Modifies server state
    
* Creates new resources
    

```basic
POST /api/users                # Create new user
POST /api/orders               # Place new order
POST /api/auth/login           # Submit login credentials
```

request example:

```basic
POST /api/users 
Host: api.example.com
Content-Type: application/json

{
    "name": "John Doe",
    "email": "john@example.com",
    "role": "user"
}
```

**Best Practices**:

1. Return 201 Created status on success
    

## 3\. PUT Method

**Purpose**: <mark>Update or replace an existing resource</mark>

**Characteristics**:

* Not safe (modifies data)
    
* Idempotent
    
* Replaces entire resource
    
* Updates existing resources
    

```basic
PUT /api/users/123             # Update user completely
PUT /api/posts/456             # Replace entire post
PUT /api/products/789          # Update product completely
```

```basic
PUT /api/users/123 
Host: api.example.com
Content-Type: application/json

{
    "name": "John Doe",
    "email": "john@example.com",
    "role": "admin",
    "status": "active"
}
```

**Best Practices**:

1. Require complete resource representation
    
2. Return 200 OK or 204 No Content
    

## 4\. PATCH Method

**Purpose**: <mark>Partial modification of a resource</mark>

**Characteristics**:

* Not safe (modifies data)
    
* Not necessarily idempotent
    
* Updates partial resource
    
* Modifies existing resources
    

```basic
PATCH /api/users/123           # Update user partially
PATCH /api/posts/456           # Update specific post fields
PATCH /api/orders/789          # Update order status
```

```basic
PATCH /api/users/123 
Host: api.example.com
Content-Type: application/json

{
    "email": "newemail@example.com",
    "status": "inactive"
}
```

**Best Practices**:

1. Accept partial updates
    

## 5\. DELETE Method

**Purpose**: Remove a resource

**Characteristics**:

* Not safe (modifies data)
    
* Idempotent
    
* Deletes existing resources
    

```basic
DELETE /api/users/123          # Delete user
DELETE /api/posts/456          # Remove post
DELETE /api/orders/789         # Cancel order
```

**Best Practices**:

1. Return 204 No Content on success
    
2. Implement soft deletes when appropriate
    
3. Verify authorization carefully
    

## Common Status Codes for Each Method

**GET**:

* 200 OK (Success)
    
* 404 Not Found (Resource doesn't exist)
    
* 403 Forbidden (No access)
    

**POST**:

* 201 Created (Success)
    
* 400 Bad Request (Invalid data)
    
* 409 Conflict (Resource conflict)
    

**PUT**:

* 200 OK (Success with response body)
    
* 204 No Content (Success without response body)
    
* 404 Not Found (Resource to update doesn't exist)
    

**PATCH**:

* 200 OK (Success with response body)
    
* 204 No Content (Success without response body)
    
* 422 Unprocessable Entity (Invalid update)
    

**DELETE**:

* 204 No Content (Success)
    
* 404 Not Found (Resource doesn't exist)
    
* 403 Forbidden (No delete permission)
    

## Conclusion

Understanding these five main HTTP methods is crucial for building RESTful APIs. Each method serves a specific purpose:

* GET for retrieving data
    
* POST for creating new resources
    
* PUT for complete updates
    
* PATCH for partial updates
    
* DELETE for removing resources
    

Choose the appropriate method based on your operation's nature and follow the best practices for each to create maintainable and secure APIs.
