POST, PUT, DELETE
POST, PUT, DELETE
Section titled “POST, PUT, DELETE”Introduction
Section titled “Introduction”Fetch supports all HTTP methods via the method option. You send data using the body option.
POST — Create
Section titled “POST — Create”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();PUT — Update
Section titled “PUT — Update”await fetch('/api/users/1', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Updated Name' })});DELETE — Remove
Section titled “DELETE — Remove”const response = await fetch('/api/users/1', { method: 'DELETE' });if (response.ok) console.log('User deleted');Summary
Section titled “Summary”- Set
methodfor POST, PUT, DELETE - Use
JSON.stringify()for JSON body - Set
Content-Type: application/jsonheader - Always handle the response status