A call to the warehouse
Client and server, URLs, HTTP methods and status codes, fetch, res.json() and a POST with JSON.
The browser is the client: it asks. The server is a program on another computer: it answers. They talk using HTTP — an agreement on what a question and an answer look like.
Where to call is given by a URL: https://warehouse.example/api/fish?type=sea
https://— the protocol;warehouse.example— the server's address;/api/fish— the path: which resource we want;?type=sea— query parameters.
If the page and the API live on the same server, the path is enough: /api/fish.
In JavaScript you make the call with fetch(url). The network is slow, so fetch immediately returns a promise — which later fulfills with a Response object. A response has a status (res.status), headers and a body — the data itself.
const res = await fetch("/api/fish");
console.log("Status:", res.status, "ok:", res.ok);
const fish = await res.json();
console.log(fish);
console.log("First fish:", fish[0].name);awaits: first we wait for the response, then for its body.Why two awaits? The response arrives in parts. First the status and headers: that's enough to fulfill the fetch promise. The body may be big and still loading, so res.json() is a promise too: it reads the rest of the body and turns the JSON text into an object (like JSON.parse).
Besides the URL, a request has a method — what we want to do:
GET— fetch something (the default in fetch);POST— create something new: an order, a comment;PUT— replace (update) something that exists;DELETE— remove it.
And a response has a status code, a three-digit number:
2xx— success:200 OK,201 Created,204 No Content("done, no body");4xx— the client's mistake:400(malformed request),401(not logged in),404(not found);5xx— the server's mistake:500("something broke on our side").
To send data, fetch takes a second argument — the request options. The body is always text, so the object is turned into JSON with JSON.stringify, and the Content-Type header tells the server how to read that text.
const res = await fetch("/api/orders", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ fishId: 1, kg: 2 }),
});
console.log("Status:", res.status);
console.log(await res.json());201 and sent the order number.API addresses usually follow the rules of REST: the URL is the "what" (a resource), the method is "what to do with it":
GET /api/fish— the list of fish;GET /api/fish/3— one fish;POST /api/orders— a new order;PUT /api/fish/3— update fish #3;DELETE /api/fish/3— take it off the shelf.
const list = document.querySelector("#fish");
const status = document.querySelector("#status");
status.textContent = "Loading…";
const res = await fetch("/api/fish");
const fish = await res.json();
status.textContent = fish.length === 0 ? "No fish" : "";
for (const item of fish) {
const li = document.createElement("li");
li.textContent = `${item.name} — $${item.price}`;
list.append(li);
}
console.log("Fish shown:", fish.length);Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.