JavaScript Academy · Lesson

Making GET Requests

Fetch data and parse JSON responses.

Lesson 1 of 413 steps

Making GET Requests is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

What fetch Does

The Fetch API makes HTTP requests from the browser and returns a Promise. It is the modern replacement for the old XMLHttpRequest.

A Basic GET

Call fetch(url) with a URL. It defaults to the GET method and resolves with a Response object.

fetch("https://api.example.com/users")
  .then((response) => {
    console.log(response.status); // 200
  });

The Response Object

The resolved value is a Response, not the data itself. It carries status, headers, and methods to read the body. You must call a body method to get the content.

fetch(url).then((response) => {
  console.log(response.ok);     // true if status 200-299
  console.log(response.status); // 200
});

Parsing JSON

Most APIs return JSON. Call response.json(), which itself returns a Promise resolving to the parsed object.

fetch(url)
  .then((response) => response.json())
  .then((data) => {
    console.log(data); // parsed JS object/array
  });

Why Two Promises

The first Promise resolves when headers arrive; the second (.json()) resolves when the full body is downloaded and parsed. That is why you chain two .then() calls.

Using async/await

Most code uses async/await for readability. Each await pauses until its Promise resolves.

async function getUsers() {
  const response = await fetch(url);
  const data = await response.json();
  return data;
}

Other Body Readers

Besides json(), a Response can be read as text(), blob() (binary), arrayBuffer(), or formData(). Choose based on what the server returns.

const response = await fetch(url);
const html = await response.text(); // raw string

Body Is Read Once

A response body is a stream you can consume only once. Calling json() then text() on the same response throws. Clone with response.clone() if you truly need both.

Query Parameters

Build query strings safely with URLSearchParams instead of concatenating by hand. It encodes special characters for you.

const params = new URLSearchParams({ page: 2, q: "hello world" });
fetch("https://api.example.com/search?" + params);

Sending Headers

Pass a second argument with options. For GET you often set an Accept header or an auth token.

fetch(url, {
  headers: {
    "Accept": "application/json",
    "Authorization": "Bearer " + token
  }
});

Returning the Promise

Wrap fetch logic in a function that returns the Promise so callers can await it. This keeps data-loading reusable across your app.

function loadUser(id) {
  return fetch("/api/users/" + id).then((r) => r.json());
}

Quick Check

GET requests with fetch.

Recap

fetch(url) returns a Promise for a Response. Read the body with json(), text(), or blob() (only once). Use async/await for clarity, URLSearchParams for query strings, and an options object for headers. Wrap it in functions that return the Promise for reuse.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Making GET Requests” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Making GET Requests”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “Making GET Requests”?

Fetch data and parse JSON responses. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Making GET Requests” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Making GET Requests
  2. POST and Sending Data
  3. Handling Errors and Status Codes
  4. Aborting Requests with AbortController
← Back to JavaScript Academy