RESTful Web APIs

Web server complexity can be roughly divided into three levels:

Static
A static server does not computation of content. It parses the request, finds the file or other pre-computed information that has the answer, and responds with the contents of that file.
Stateless

A stateless server does compute part of the content, but does so based only on the current request; requests are not combined into sequences and do not otherwise interact with one another. Stateless usually also contain some static information. Static servers are a trivial subset of stateless servers.

The stateless term is somewhat misleading: the server does maintain state and requests can modify that state, but certain types of state are not stored; see More on State for more.

Stateful
A stateful server can track of different information for different clients. Often, this looks like a log-in action followed by a series of actions that interact with the logged-in user’s data, then a log-out action. Stateful servers usually also contain some stateless and static information. Stateless servers are a trivial subset of stateful servers.

A particularly popular form of stateless server designs are web APIs that use the REST architectural style, also called REST APIs or RESTful Web APIs.

1 REST

REST officially an abbreviation for representational state transfer, though that longer term is rarely used. REST includes 5 properties:

2 RESTful Web API

In general, RESTful web APIs have the following properties:

3 More on State

A key idea of REST is that programs are stateless, but that does not mean they lack any state. Even databases, which are apps whose sole purpose is to manipulate state, can be exposed through a REST API. To understand what we mean by stateless in a REST sense, let’s consider a few kinds of state1 These are not universal terms, just distinctions that I hope will be helpful in explaining how people talk about REST:

Consider an IDE like VS Code.

Consider an course registration system like Banner.

The main stateless aspect of REST is that a RESTful service

  1. Does not have user-specific navigational state at all.
  2. Embodies user-specific fundamental state in some kind of token or value passed in by the user with each request, not in any kind of memory about what messages came over this TCP/IP connection in the past.

Terminals are not RESTful. If I send a terminal the command ls, what it will send back is dependent on my history of commands with this terminal connection. If I send it cd .., and then send it ls again I’ll get a different result despite not having changed any durable state.

If you send cd .. to my computer, nothing about my experience will change2 If you send it rm my experience will change, but that’s because rm changes durable user-independent state, unlike cd.. If I open a dozen terminals, the cd commands I send to one do not impact the others. Each terminal session has its own user navigational state that the others cannot impact.

Our course file upload system is RESTful. To upload a file you send a single HTTP request3 Typically you don’t send this manually; upload.php handles constructing this HTTP request for you. with the following parts:

There’s no navigational state needed by the server: the full information about who you are and what MP you are uploading for is included in your request. There is some user state, in the form of a tool for verifying username/password pairs and a list of random session IDs with what user each maps to, but that state is passed in with each new request.

If someone else could figure out what HTTP headers you are using, they could act on your behalf. That’s part of why we use HTTPS instead of plain HTTP for that page: HTTPS encrypts the entire HTTP, making it prohibitively difficulty to read the headers even if you are part of the chain of computers that deliver the message’s HTTP packets.

Our course file upload webpage is not RESTful.

Although it uses the RESTful upload system, it also has user navigational state: whether the file upload dialog is visible or not, what files you’ve chosen to upload, whether the files have been sent to the server, whether the server has responded: these are all stored by the webpage and used to update the display and make interacting with the back-end upload service more user-friendly.