Basic GET Request

fetch('/api/data')
  .then(response => response.json()) // Convert response to JSON
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

GET Request with Headers

fetch('/api/data', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN'
  }
})
  .then(response => response.json())
  .then(data => console.log(data));

POST Request with JSON Body

fetch('/api/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'John Doe', age: 25 })
})
  .then(response => response.json())
  .then(data => console.log(data));

PUT Request (Updating Data)

fetch('/api/users/1', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'Jane Doe', age: 30 })
})
  .then(response => response.json())
  .then(data => console.log(data));

PATCH Request (Partial Update)

fetch('/api/users/1', {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ age: 35 }) // Only update `age`
})
  .then(response => response.json())
  .then(data => console.log(data));

DELETE Request

fetch('/api/users/1', { method: 'DELETE' })
  .then(response => response.json())
  .then(data => console.log('Deleted:', data));