Update with PUT

PUT replaces an item. The id is in the URL, the new data is in the body.

AJAX · Update with PUTBeginner
async function updateUser(id, data) {
  const res = await fetch('/api/users/' + id, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
  });
  if (!res.ok) throw new Error('Update failed');
  return res.json();
}

updateUser(7, { name: 'Ali Khan' });
fetchputupdatejson