JavaScript: Response

A Response object contains the status and body returned by fetch().

What you will learn

Minimal example

fetch("data.json")
  .then(response => {
    if (!response.ok) throw new Error(`HTTP ${response.status}`);
    return response.json();
  })
  .then(data => {
    console.log("Data received:", data);
  })
  .catch(error => {
    console.error("Could not load data:", error);
  });

The first callback receives the Response object. Check the status before reading the body, then return the Promise from json() or text() so the next step receives the converted value.

Useful properties and methods

Reading a body consumes it. Choose the method that matches the format you need, and do not call both on the same response unless you clone it first.