# Activity 29: HTTP Methods

### 1\. **GET** Method

**Purpose**: The `GET` method is used to retrieve data from a server. It does not modify the server's state and is idempotent (multiple requests yield the same result).

**Use Case**: Retrieving user data, product lists, or any resource without changing server data.

**Example**:

```plaintext
javascriptCopy codeconst axios = require('axios');

axios.get('https://example.com/api/users')
  .then(response => {
    console.log('Data retrieved:', response.data);
  })
  .catch(error => {
    console.error('Error fetching data:', error);
  });
```

### 2\. **POST** Method

**Purpose**: The `POST` method is used to send data to the server to create a new resource. This method is not idempotent (multiple identical requests will create multiple resources).

**Use Case**: Creating a new user, adding a new item to a database, submitting a form.

**Example**:

```plaintext
javascriptCopy codeconst axios = require('axios');

axios.post('https://example.com/api/users', {
  name: 'John Doe',
  email: 'johndoe@example.com'
})
  .then(response => {
    console.log('New resource created:', response.data);
  })
  .catch(error => {
    console.error('Error creating resource:', error);
  });
```

### 3\. **PUT** Method

**Purpose**: The `PUT` method is used to update an existing resource or create it if it does not exist. It typically replaces the entire resource with the provided data and is idempotent.

**Use Case**: Updating a user’s profile, replacing an entire document in a database.

**Example**:

```plaintext
javascriptCopy codeconst axios = require('axios');

axios.put('https://example.com/api/users/1', {
  name: 'Jane Doe',
  email: 'janedoe@example.com'
})
  .then(response => {
    console.log('Resource updated:', response.data);
  })
  .catch(error => {
    console.error('Error updating resource:', error);
  });
```

**Difference from POST**:

* **PUT** is used to update or create a resource at a known URI and replaces the entire resource.
    
* **POST** creates a new resource and can be used at a general endpoint (e.g., [`https://example.com/api/users`](https://example.com/api/users)), allowing the server to determine the URI of the new resource.
    

### 4\. **DELETE** Method

**Purpose**: The `DELETE` method is used to remove a resource from a server and is idempotent (deleting a resource multiple times has the same effect as deleting it once).

**Use Case**: Removing a user from a database, deleting a post.

**Example**:

```plaintext
javascriptCopy codeconst axios = require('axios');

axios.delete('https://example.com/api/users/1')
  .then(response => {
    console.log('Resource deleted:', response.data);
  })
  .catch(error => {
    console.error('Error deleting resource:', error);
  });
```

### 5\. **PATCH** Method

**Purpose**: The `PATCH` method is used to apply partial updates to a resource. Unlike `PUT`, which updates the entire resource, `PATCH` modifies only specified fields. It is also idempotent.

**Use Case**: Updating a user's email or a specific field of a resource without changing the entire record.

**Example**:

```plaintext
javascriptCopy codeconst axios = require('axios');

axios.patch('https://example.com/api/users/1', {
  email: 'newemail@example.com'
})
  .then(response => {
    console.log('Resource partially updated:', response.data);
  })
  .catch(error => {
    console.error('Error updating resource:', error);
  });
```

**Comparison with PUT**:

* **PATCH**: Used for partial updates (e.g., changing one
