Design a resource-oriented interface with representations, HTTP methods, and explicit cache rules.
Lesson goal
Follow a user resource from creation to retrieval and explain how different servers or caches can serve the same contract.
Before you begin — API contracts.
A client needs to create a user, retrieve that user’s profile, and later change the profile. Representational State Transfer (REST) organizes these interactions around resources. In an HTTP API, a URI identifies the resource, a method expresses the operation, and a representation carries information about its state.
The client sends POST /api/v1/users with a JSON body containing Alice’s name and email. The target is the collection of users. The server assigns ID 42, creates Alice’s account, and returns 201 Created with Location: /api/v1/users/42. That URI identifies the individual user.
These examples illustrate the same exchange against a sample API. Each sets the request’s media type and reads the location from the response.
const response = await fetch("https://api.example.com/api/v1/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Alice", email: "[email protected]" }),
});
if (response.status === 201) {
console.log(response.headers.get("Location"));
}The client can now send GET /api/v1/users/42. Suppose the service supports JSON and XML. The diagram follows two clients requesting the same user. Each asks for a media type the service supports.
The JSON response includes the selected media type and the header used to choose it.
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Accept
{"id":42,"name":"Alice"}A client requesting XML at the same URI can receive <user id="42"><name>Alice</name></user> with Content-Type: application/xml. Both clients are reading the same resource. Content negotiation lets the server select a representation. Accept expresses a preference, Content-Type identifies the actual format, and Vary: Accept tells caches that this request header factors into the selection. HTTP content negotiation
| Intent | Example | What the server reads |
|---|---|---|
| Replace the profile | PUT /api/v1/users/42 | The desired complete representation under this contract. |
| Change one field | PATCH /api/v1/users/42 | A patch describing the change. |
| Remove the resource | DELETE /api/v1/users/42 | The target URI and the caller’s authority to remove it. |
A message is self-descriptive when the method or status, headers, and representation give the recipient the information it needs to interpret it. For example, 204 No Content tells the client that an update succeeded and there is no response body to parse.
REST also uses hypermedia controls, sometimes called HATEOAS. A representation can include a link to /api/v1/users/42/orders under a documented relation such as orders. A client that understands that relation can follow the link the server supplied. REST uniform interface
Statelessness means each request carries the context needed to process it, including the target and any required credentials. Suppose one server creates user 42 and another receives the profile request. The second can check the credentials and load user 42 from shared storage. It can do this without relying on a conversation remembered by the first server. The user’s stored account is resource state and can persist across requests.
Responses also communicate whether a cache may reuse them. For a response intended only for one user’s browser, Cache-Control: private, max-age=60 permits a private cache to reuse it while fresh and prevents a shared cache from storing it. For a resource that may be revalidated, an ETag lets a client ask whether its stored representation has changed. HTTP caching
A mobile app requests user 42 as JSON. Another client requests the same URI as XML. What could go wrong if a shared cache stored the JSON reply for both requests using only the URI?
The second client could receive the wrong representation. For a shareable response chosen using Accept, Vary: Accept tells the cache to account for that header before reusing a stored response. A private response belongs outside the shared cache altogether.