JavaScript Tutorial: HTTP & fetch
Make HTTP requests with fetch, handle GET/POST/PUT/DELETE, work with headers, authentication, and graceful error handling.
Connect to the world
Every modern app talks to APIs. `fetch` is the browser and Node.js native HTTP client — clean, Promise-based, and powerful.
After this chapter, you'll be able to talk to any REST API — GitHub, Stripe, Twitter, your own backend.
What you'll learn in this JavaScript http & fetch tutorial
This interactive JavaScript tutorial has 8 hands-on exercises. Estimated time: 22 minutes.
- fetch — make your first HTTP request — `fetch(url)` returns a Promise that resolves to a `Response` object. Call `.json()` on it to parse the body.
- Error handling — network vs HTTP errors — `fetch` only rejects for **network failures** (no connection, DNS failure). HTTP errors (404, 500) still resolve — you g…
- POST — send data — POST requests send data in the body. You need to:
- Query parameters with URLSearchParams — Build query strings with `URLSearchParams` — it handles URL encoding automatically:
- Request headers and Authorization — APIs often require authentication via headers:
- AbortController — cancel requests — `AbortController` cancels in-flight requests — essential for:
- Parallel requests with Promise.all — From Chapter 17: `Promise.all` runs Promises in parallel. Applied to fetch: load multiple API resources simultaneously.
- Build a mini HTTP client library — Final challenge — no starter code.
JavaScript HTTP & fetch concepts covered
- Connect to the world