HTTP requests and Fetch

ECMAScript 2026

HTTP is a set of rules for exchanging requests and responses. A request names a destination and a method. A response includes a status code and may include a body. An API is an agreed way for a program to ask a service for data or an action.

On this page

What is JSON?

JSON is a text format for exchanging data. A JSON object uses double quotes around property names and string values. JSON.parse reads JSON text into a JavaScript value, while JSON.stringify writes a value as JSON text.

How to try it Create a folder for these examples and save the code as product.json.

{ "name": "Pen", "price": 2500 }

What to expect You now have a small JSON file for the GET example.

Read data with a GET request

GET asks for a resource without making a change to it. fetch() returns a promise for a Response. Status codes in the 200–299 range give response.ok === true. A server response such as 404 can still fulfill the fetch promise, so check ok before reading the body. response.json() returns a promise for the parsed data.

How to try it Save as index.html beside product.json. Serve both using VS Code Live Server or run python -m http.server 8000 in that folder and open http://localhost:8000.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>GET demo</title>
</head>
<body>
  <p id="result">Loading…</p>
  <script>
    async function loadProduct() {
      const result = document.querySelector("#result");
      try {
        const response = await fetch("./product.json");
        if (!response.ok) throw new Error("HTTP " + response.status);
        const product = await response.json();
        result.textContent = product.name + ": " + product.price;
      } catch (error) {
        result.textContent = "Could not load the product: " + error.message;
      }
    }
    loadProduct();
  </script>
</body>
</html>

What to expect The page shows Pen: 2500. Change product.json to missing.json in the request, reload, and look for HTTP 404 in the message.

If the result differs If the address starts with file:, use the local server URL. If you see 404, check the filename and the folder the server is serving.

Send JSON with a POST request

POST submits data for the server to process. Here method selects POST, the Content-Type header describes the body format, and JSON.stringify produces that text. This demo API returns a simulated creation response.

How to try it Save as post.html in the GET example folder. Open it through the same local server while connected to the internet, and click Send example post.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>POST demo</title>
</head>
<body>
  <button id="submit" type="button">Send example post</button>
  <p id="result" role="status"></p>
  <script>
    const button = document.querySelector("#submit");
    const result = document.querySelector("#result");
    button.addEventListener("click", async () => {
      result.textContent = "Sending…";
      try {
        const response = await fetch("https://jsonplaceholder.typicode.com/posts", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ title: "Pen", body: "A sample product post", userId: 1 }),
        });
        if (!response.ok) throw new Error("HTTP " + response.status);
        const data = await response.json();
        result.textContent = "Received: " + data.title + " (ID " + data.id + ")";
      } catch (error) {
        result.textContent = "Could not send: " + error.message;
      }
    });
  </script>
</body>
</html>

What to expect The demo API replies with the title and a simulated new ID. The page shows “Received: Pen (ID 101)” if the service responds as documented.

Official references