Skip to content

POST, PUT, DELETE

Fetch supports all HTTP methods via the method option. You send data using the body option.

const response = await fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
},
body: JSON.stringify({
name: 'Alice',
email: 'alice@email.com'
})
});
const newUser = await response.json();
await fetch('/api/users/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Updated Name' })
});
const response = await fetch('/api/users/1', { method: 'DELETE' });
if (response.ok) console.log('User deleted');
  • Set method for POST, PUT, DELETE
  • Use JSON.stringify() for JSON body
  • Set Content-Type: application/json header
  • Always handle the response status