The Fetch API is the modern, built-in way to make HTTP requests in JavaScript. It replaced XMLHttpRequest (XHR) with a cleaner Promise-based API. Basic Usage fetch(url, options) returns a Promise that resolves to a Response object when the server responds. Critically: fetch only rejects on network errors (no connection, DNS failure). An HTTP 404 or 500 response still resolves — you must manually check response.ok or response.status. Reading the Response Body response.json() parses JSON and returns a Promise. response.text() returns the body as a string. response.blob() returns binary data (images, files). These are all async and must be awaited. Request Options The options object configures the request: method (GET, POST, PUT, DELETE, PATCH), headers (request headers like Content-Type…
Key Takeaways
fetch() only rejects on network errors, not HTTP 4xx/5xx.
Always check response.ok before calling response.json().
Set Content-Type: application/json for POST/PUT with JSON body.
Use AbortController to cancel in-progress requests.