Web server complexity can be roughly divided into three levels:
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.
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.
REST officially an abbreviation for representational state transfer
, though that longer term is rarely used. REST includes 5 properties:
In general, RESTful web APIs have the following properties:
Clients send HTTP requests; servers reply.
The HTTP request method describes if/how the request changes server state. The two most common methods used are
GET
does not change server statePOST
changes existing server stateSometimes PUT
is used to create new server state and DELETE
to remove existing server state, but sometimes they are both handled using POST
instead. Other HTTP request methods are relatively rarely used in web APIs.
The HTTP request path identifies what resource you are accessing.
The HTTP request body (which is not used by GET
requests) describes specific changes to be implemented.
HTTP headers may be used to pick one of several equivalent response formats, but should not be used to change the meaning of the request.
All request bodies within a single API use the same format; often JSON or XML, but sometimes the simpler but less flexible application/x-www-form-urlencoded
and multipart/form-data
formats used by HTML forms and query strings.
All response bodies that provide structured data structure it the same way, often JSON or XML.
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
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:
?f=
followed by a string consisting of a student NetID like luthert
, an MP identifier like mp7
, and a file name like chunkserver.py
, separated by slashes.Authorization:
with a value that is an encrypted version of a netid and password.Cookie:
with a value that encodes a dict
, part of the contents of which is a random session ID.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.