REST APIs and JSON Explained with Examples
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.
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.
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.
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.