Course contents · Lesson 4 of 17

REST APIs and JSON Explained with Examples

How Apps Are Built · Lesson 4 of 17 · 9 min Foundations

Tap one of your orders in a shopping app, and its details appear in under a second. The app asked the shop's server for that one order and got a short block of text back. This lesson explains REST APIs and JSON: how a request names a thing and an action, what the status code says, and how to read the data.

REST: every thing has an address

An API, as What Is an API? explained, is a defined way for one program to ask another for work or data. REST (Representational State Transfer) is the most common style for designing web APIs, and an API that follows it is called a REST API.

REST treats each thing the API knows about as a resource with its own URL (web address). A collection of users might live at /users, and user 42 at /users/42. The URL names a thing, never an action: there is no /deleteUser42.

The action goes in the method, the verb that starts every request in HTTP (HyperText Transfer Protocol), as What Is HTTP? showed. Apps mostly need four actions, known as CRUD: create, read, update and delete. The table maps each to a method.

Table of the four CRUD actions for an illustrative users API. Create uses POST on /users: it adds a user and the server picks the ID; not safe to repeat. Read uses GET on /users/42: it sends back user 42; safe to repeat. Update uses PUT on /users/42: it replaces the whole of user 42; safe to repeat. Update can also use PATCH on /users/42: it changes only the fields sent; not always safe to repeat. Delete uses DELETE on /users/42: it removes user 42; safe to repeat. A note says /users is the list and /users/42 is one user in it
The URL names the thing; the method decides what happens to it.

Notice that one address, /users/42, does four jobs depending on the method. PUT replaces the whole user with what you send, while PATCH changes only the fields you send, such as a new city. POST goes to the collection, because the server picks the new user's number.

On the slip, add is POST, borrow is GET, return (put it back at its address) is PUT and discard is DELETE. The slip bends in one place: borrowing takes the book away, but a GET leaves the resource where it was. You receive a representation, a copy in a format such as JSON, which is the "representational" in REST.

Status codes: the stamp on the reply

The first line of every response carries a three-digit status code, whose first digit names the family: 2xx success, 3xx a redirect (ask at another address), 4xx the caller's mistake and 5xx the server's problem. A 4xx keeps coming back until you fix the request; after a 5xx, trying again later can work.

REST APIs choose codes precisely, so a program can react before reading the body. 200 OK answers a successful GET, and 201 Created a POST that made something. 204 No Content means success with nothing to send back, common after a DELETE. 400 Bad Request means the server could not accept what you sent, such as broken JSON; 404 Not Found, that nothing lives at that URL.

JSON: data as plain text

Most REST APIs send data as JSON (JavaScript Object Notation), the curly-brace text from the try-it in Frontend and Backend. Despite its name, it is plain text that almost any programming language can read and write.

An object, in curly braces, holds name–value pairs: a name in double quotes, a colon, a value, and commas between pairs. An array, in square brackets, holds an ordered list. A value is a string of text in double quotes, a number, true or false (a boolean, a yes-or-no value), null for "no value", or another object or array. In the diagram, notice the object inside the object: that is how JSON describes something with parts.

An illustrative JSON object for one user, with each kind of value labelled. The whole thing is an object: name and value pairs inside curly braces. id: 42 is a number, digits with no quotes. name: Asha in quotes is a string, text in double quotes. active: true is a boolean, true or false with no quotes. email: null is null, deliberately no value. skills, holding SQL and Git in square brackets, is an array, an ordered list. address holds a nested object, an object used as a value, with more name and value pairs inside, here city: Pune. The final closing brace ends the object. A note says names always go in double quotes and commas go between pairs
The punctuation tells you each value's kind, and objects can nest.

Quotes matter: 42 is a number, but "42" is a string that happens to hold digits, and a program expecting one may reject the other. Single quotes are an error in JSON, and so is a comma after the last pair.

Headers and bodies

Alongside the method and URL, a request carries headers: Name: value lines that add details, like notes on the slip. Content-Type says what format the body is in; for JSON, it is application/json.

The body is the data itself. POST, PUT and PATCH requests carry one; GET and DELETE usually do not. A response usually has a body, the data or an error message, with its own Content-Type; 204 No Content is the exception.

The flowchart follows one illustrative request that creates a user: read the left column, then the right. Both messages share one shape, and the Location header in step 5 gives the new user's address.

Step-by-step flowchart of an illustrative request that creates user 42. Left column, the request from client to server. Step 1, method and URL: POST /users, the action, then which resource. Step 2, headers: Host: api.example.com and Content-Type: application/json, which site, and that the body is JSON. Step 3, body: the JSON name Asha, the data for the new user. An arrow carries the request to the server, which saves it, then on to the right column, the response from server to client. Step 4, status line: HTTP/1.1 201 Created, success, a new resource was made. Step 5, headers: Content-Type: application/json and Location: /users/42, the body format and the new user's address. Step 6, body: the JSON id 42 and name Asha, the new user as the server saved it. A banner says both have the same shape: first line, headers, then a body
Request and response share one shape: first line, headers, body.

Reading a response, and safe retries

To read any response, go in order: the status code (did it work?), then Content-Type (is it JSON?), then the body, where you find the names you need and check each value's kind.

A method is idempotent when sending the same request twice leaves the server in the same state as sending it once. GET, PUT and DELETE are: deleting user 42 twice still leaves it deleted, even if the second reply is a 404, just as returning a book twice leaves one copy on the shelf.

POST is not: the same POST sent twice can create two users, or two payments. Nor is PATCH guaranteed to be, since "add 10 points" applied twice adds 20. So when a request times out and you cannot tell whether it arrived, resending a GET, PUT or DELETE is safe; resending a POST needs care.

Check yourself

An app sends PATCH to /users/42 with only a new city. What changes, and why could PUT with that body be worse?

Only the city, because PATCH updates just the fields sent. PUT replaces the whole user, so the fields you left out could be wiped, or the request rejected.

You tap Pay, the signal drops, and the app cannot tell whether its POST arrived. Why is resending riskier than resending a GET?

POST is not idempotent: if the first one arrived, a second makes a second payment. A GET only reads, so repeating it changes nothing.

An API answers 200 OK with a body saying the user was not found. Why is that poor design?

Programs check the status first, and 200 says success, so they would treat the failure as data. A 404 would say what actually happened.