# fetch JSON with error handling
URL: https://wwadd.dev/moves/javascript/fetch-json

`fetch` only rejects on network failure — a 404 or 500 still resolves. Always check `res.ok` yourself.


```javascript
async function getUser(id) {
  const res = await fetch(`/api/users/${id}`, {
    headers: { Accept: "application/json" },
  });
  if (!res.ok) {
    throw new Error(`Request failed: ${res.status}`);
  }
  return res.json();
}

// POST with a body
await fetch("/api/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Ada" }),
});
```

## Related Moves
- [Promises and async patterns](https://wwadd.dev/moves/javascript/promises)
- [HTTP status codes worth memorizing](https://wwadd.dev/moves/git-http/status-codes)

