HTTP Methods (GET, POST, PUT, DELETE)
REST APIs use HTTP methods to define what action you want to perform. Think of them like verbs: GET reads, POST creates, PUT updates, DELETE removes.
// GET: fetch data
fetch('/api/users')
.then(res => res.json())
.then(users => console.log(users))
// POST: create data
fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Daniel', age: 20 })
})
// PUT: update data
fetch('/api/users/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ age: 21 })
})
// DELETE: remove data
fetch('/api/users/1', { method: 'DELETE' })