All tutorials
Advanced

Fetch API

Make HTTP requests from the browser without any library.

1. Explanation

fetch(url, options) returns a Promise that resolves to a Response. Call .json() on the response to read the body. fetch only rejects on network failure — a 404 still resolves, so check response.ok. Pass method, headers and body in options for POST requests.

2. Example

Try it yourself
Console
Click Run to see output.

3. Expected output

output
Leanne Graham Sincere@april.biz

4. Common mistakes

  • Forgetting to await response.json()
  • Not checking response.ok for HTTP errors
  • Sending an object as body without JSON.stringify and the right Content-Type

5. Practice questions

  1. Fetch a list of posts and log their titles
  2. POST a new todo to a test API
  3. Handle a network error with try/catch
→ Try more practice problems