Skip to content

HTTP & fetch()

The Course
•
20 min study

From a local function to a service

An HTTP request is a message from a client (here, your browser) to a server asking for a resource or an action. The server sends a response with a status code and, often, a body containing data. A URL identifies where to send the request. An API is an agreed interface: which URL to call, what input to send, and what response shape to expect.

For a read, fetch("/books") uses GET by default and asks the current site's server for /books. fetch is provided by the browser; request in our example is just a parameter name we chose. Passing fetch as that argument uses the real operation. Passing a controlled async function lets us practice the same steps without contacting a server.

Common statuses include 200 for success, 404 for a missing resource, and 503 for temporary service unavailability. A network failure means no usable response arrived; a 503 response is a different outcome. A JSON body is still text until its parsing step succeeds.

A fulfilled fetch is not necessarily a successful response

Fetch makes a request and returns a promise for a Response. Network failures can reject it, while HTTP statuses such as 404 or 503 normally fulfill it. Check response.ok before using the body. .ok is true for status codes in the 200–299 range; status contains the numeric code.

Reading the body is another asynchronous operation: await response.json() to parse JSON. That can reject if the body is not valid JSON. It can also produce valid JSON with the wrong application shape. Status check, body parsing, and domain validation are separate steps. A Response body is normally consumed only once: store the parsed value if you need to inspect it again rather than calling response.json() a second time.

The function below accepts request as a parameter. Passing fetch in a real browser would make the HTTP call. Here a local async function returns a test double, an object implementing only the Response fields the code uses. It models the contract without using a live service. A test double is not a full browser Response and does not test whether a real server permits the browser request. CORS is the browser-enforced permission mechanism for reading responses from another origin (a different scheme, host, or port); the server must grant that permission. Client-side validation cannot grant it.

An adapter translates an external API into the small interface your app needs. Keep raw HTTP details there; the model should receive validated books rather than browser Response objects. Do not automatically retry writes: the server may have applied a write even if the response was lost.

Predict and trace

Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.

Predict, then run JavaScript
async function main() {
  async function readJson(request, url) {
    const response = await request(url);

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    return await response.json();
  }

  const ok = async () => ({
    ok: true,
    status: 200,
    json: async () => ["Notes"]
  });
  const bad = async () => ({ ok: false, status: 503, json: async () => [] });

  console.log((await readJson(ok, "/books"))[0]);

  try {
    await readJson(bad, "/books");
  } catch (error) {
    console.log(error.message);
  }
}

main();
Check your prediction
Text
Notes
HTTP 503

The bad response fulfills the request promise but fails the explicit status rule. Its body is not consumed.

Debug a plausible mistake

Exercise

Repair HTTP failure handling

Print HTTP 503 for the failed mock response. Do not treat its JSON error body as a successful title. Then test ok=true and explain which branch reaches json(). No network access is needed.

Predict the supplied program's actual behavior first. It runs without an error, but breaks the requirement. Make a small repair, then try a case that would distinguish the repaired version from the original.

Debugging workspace JavaScript
const response = {
  ok: false,
  status: 503,
  json: async () => ({ title: "Maintenance" })
};

async function loadTitle() {
  return (await response.json()).title;
}

async function main() {
  try {
    console.log(await loadTitle());
  } catch (error) {
    console.log(error.message);
  }
}

main();
Hint 1: the idea

A fulfilled fetch promise can still describe an unsuccessful HTTP status.

Hint 2: a first step

Check the response status policy before parsing its successful payload.

Show one solution
JavaScript
const response = {
  ok: false,
  status: 503,
  json: async () => ({ title: "Maintenance" })
};

async function loadTitle() {
  if (!response.ok) {
    throw new Error("HTTP " + response.status);
  }
  return (await response.json()).title;
}

async function main() {
  try {
    console.log(await loadTitle());
  } catch (error) {
    console.log(error.message);
  }
}

main();
Text
HTTP 503

The original printed Maintenance: it parsed the body without asking whether the response was successful, so a 503 error page became a book title. fetch fulfills for every HTTP status, so the adapter must check response.ok itself before json(). With ok: true the check passes and the same code returns the title, which proves the guard, not the parsing, changed the outcome.

Trace the contract before building

A fetch rejection and an unsuccessful HTTP status are different failure paths. A response can arrive successfully with status 503, so you still need an explicit response.ok policy before treating its body as successful data.

Trace transport, status, body decoding and domain validation as separate boundaries. A JSON body from a failed request may itself be syntactically valid; that does not make it the requested catalogue. Use mock responses to exercise each branch without depending on a public service. Keep useful error categories rather than collapsing every failure into an empty successful list.

Build it yourself

Exercise

HTTP & fetch(): practice

Write loadCount(request), requesting /count, rejecting non-ok status, then returning body.count. Print 3 for a controlled successful response and a message for a rejected request.

Your working space JavaScript
// This first adapter assumes a valid count body after parsing.
Hint 1: the idea

Check response.ok and await response.json separately.

Hint 2: a first step

Let a request rejection reach the caller’s try/catch.

Show one solution
JavaScript
async function main() {
  async function loadCount(request) {
    const response = await request("/count");

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    const body = await response.json();

    return body.count;
  }

  console.log(
    await loadCount(async () => ({
      ok: true,
      json: async () => ({ count: 3 })
    }))
  );

  try {
    await loadCount(async () => {
      throw new Error("Network unavailable");
    });
  } catch (error) {
    console.log(error.message);
  }
}

main();
Text
3
Network unavailable

Make json throw an Error("Invalid JSON"). Confirm it is a third failure path distinct from network and status errors. Next lesson adds body validation.

Test a changed requirement

Make json throw an Error("Invalid JSON"). Confirm it is a third failure path distinct from network and status errors. Next lesson adds body validation.

Explain before moving on

Why can HTTP 503 reach the success path of await request? What does the double not test about a real service?

Going further

Read MDN’s Fetch guide after working through the examples. Use it as a reference when you meet an unfamiliar option; finishing every reference section is not required for this course.

Was this article helpful?

Your response is saved on this device.