35 min·intermediate

Lesson 6

HTTP, JSON & REST

Build a practical mental model for http, json & rest and use it in working JavaScript.

By the end, you will be able to…

  • Explain http, json & rest in your own words
  • Use http, json & rest in a small program
  • Recognize and debug a common failure mode

Build the mental model

REST APIs expose resources at URLs and use HTTP methods (GET, POST, PUT, DELETE) to act on them, returning status codes that describe the outcome. JSON is the near-universal format for the request/response bodies.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: { id: 7, name: "Ada" }

Why this matters

Nearly every real app talks to at least one REST API; reading status codes and JSON shapes correctly is a daily skill.

Real-world example

A weather app's request to a forecast API and the JSON it parses back is a textbook example of this exact protocol.

Common mistakes

  • Assuming every response is JSON without checking the content type
  • Not distinguishing 2xx (success), 4xx (client error), and 5xx (server error) status ranges
  • Sending a POST body without setting the Content-Type header

Try it yourself

Challenge

Write describeStatus(code) that classifies an HTTP status into success/client error/server error.

Practice

Knowledge check

What HTTP status range indicates a client error, like a bad request?

Summary

REST resources live at URLs and respond over HTTP with status codes and (usually) JSON bodies.