Chapters
- 1. Identifier Design with URIs
- 2. Interaction Design with HTTP
- 3. Metadata Design
- 4. Representation Design
- 5. Client Concerns
- 6. Final Thoughts
Web Architecture
| Constraint | Core idea | Main benefit |
|---|---|---|
| Client-server | Separate the roles of client and server | Independent development and deployment |
| Uniform interface | All components talk through standardized interfaces | Consistent interoperability |
| Layered system | Intermediaries can sit between client and server | Security, caching, load balancing |
| Cache | Servers declare which responses are cacheable | Lower latency and server load |
| Stateless | Server keeps no client session state | Scalability |
1. Client-server
- Concerns are separated: clients and servers each have a distinct role.
- Each side can be built and deployed independently, in any language or technology.
- The only requirement is that both follow the uniform interface.
2. Uniform interface
This is the central constraint. If any component strays from the standards, communication breaks down. It consists of four sub-constraints.
a. Identification of resources
- Every distinct concept on the Web is a resource with a unique identifier, such as a URI.
- Example: a site’s home page URI identifies that site’s root resource.
b. Manipulation of resources through representations
- Clients work with representations, never the resource itself.
- One resource can have multiple representations (for example HTML for a browser, JSON for a program) without its identifier changing.
c. Self-descriptive messages
- Requests can carry a desired state, and responses carry the current state.
- The server decides whether to accept or deny a requested change.
- Metadata (resource state, format, size, message details) travels in uniform fields such as HTTP headers.
d. Hypermedia as the engine of application state (HATEOAS)
- Representations include links to related resources.
- Links let users navigate in a directed way, and the presence or absence of a link is part of the resource’s current state.
3. Layered system
- Network intermediaries such as proxies and gateways can be inserted transparently.
- They typically intercept traffic for a specific purpose: security enforcement, response caching, or load balancing.
4. Cache
- Servers must declare whether each response can be cached.
- Benefits: lower perceived latency, higher availability and reliability, and reduced server load, which lowers the overall cost of the Web.
- Caches can live anywhere on the path: inside an organization’s network, in a CDN, or in the client.
5. Stateless
- The server does not need to remember the state of client applications.
- Each request must carry all the context the server needs.
- Trade-off: clients take on more complexity, but servers can handle far more clients, which is a key source of the Web’s scalability.
6. Code-on-demand (optional)
- Servers may temporarily send executable code, such as scripts or plug-ins, to clients.
- Downside: it couples server and client technology, since the client must be able to run the code.
- That coupling is why it is the only optional constraint.
- Examples: Java applets, JavaScript, Flash.
Key takeaways
- Decoupling (client-server, uniform interface) lets each part evolve on its own.
- Scalability comes mainly from statelessness, caching, and layered intermediaries.
- Standardization is what holds the system together, so deviating from the uniform interface is the most damaging mistake.
- Optionality applies only to code-on-demand, because it adds coupling.
REST API Design
Designing a REST API often feels more like an art than a science. Some best practices are implicit in HTTP, while others are pseudo-standards that emerged over time. The book offers a set of design rules that answer recurring questions with consistency, so clients can rely on them. The rules can be adopted as a full set or a la carte. Chapter 1 recaps the Web’s invention and stabilization, motivates the rule-based approach, and introduces WRML, a conceptual framework for uniform REST API design.
Rules questions
- When should URI path segments use plural nouns?
- Which request method should update resource state?
- How do I map non-CRUD operations to URIs?
- Which HTTP status code fits a given scenario?
- How do I manage versions of a resource’s state representations?
- How should a hyperlink be structured in JSON?
Vocabulary, regrouped by theme
1. Architecture and REST basics
| Term | Meaning |
|---|---|
| API | Exposes data and functions so programs can interact |
| Architectural constraint | Limits component behavior to enforce uniformity and achieve a desired property |
| Architectural style | Fielding’s term for a set of constraints on interconnected components |
| REST | Fielding’s derivation of the Web’s architectural style |
| REST API / Web API | A web service interface conforming to the Web’s architectural style / used by clients to interact with a web service |
| Scalability | Gracefully handling increasing workload |
| WRML | Conceptual framework for designing and implementing uniform REST APIs |
2. REST constraints
| Term | Meaning |
|---|---|
| Client-server | Separates concerns so each side evolves independently |
| Stateless | Server holds no client-specific state, so it can support more clients |
| Cache | Lets intermediaries hold representations to help servers meet demand |
| Layered system | Intermediaries sit between client and server without breaking the uniform interface |
| Uniform interface | Four constraints that standardize communication between components |
| HATEOAS | A state-aware list of links conveying a resource’s available actions |
| Code-on-demand | Optional: server transfers executable programs to clients as needed |
3. Resources and representations
| Term | Meaning |
|---|---|
| Resource | Any Web concept with a unique identifier, manipulated via the uniform interface |
| Resource identifier / URI | Universally unique ID of a concept / the syntax Berners-Lee invented for it |
| Resource model | An assembly of interlinked Web concepts |
| Representation (resource state representation) | The formatted state of a resource, transferred between client and server |
| Media type | Syntax describing the form of content |
4. Messages
| Term | Meaning |
|---|---|
| Message | Self-descriptive envelope, often carrying a representation |
| Request message | Sent by clients to a URI; may carry a suggested state |
| Response message | Returned by servers with the result; may carry a state |
| Entity body | Part of an HTTP message holding optional content |
| Entity headers | Part of an HTTP message carrying metadata about the resource and representation |
5. Web technologies
| Term | Meaning |
|---|---|
| Hypertext | Documents with embedded links, forming a navigable mesh |
| Hypermedia | Hypertext extended to multiple formats tied together by links |
| HTML | Berners-Lee’s markup language for a resource’s information and relationships |
| HTTP / HTTP/1.1 | Message-based protocol for computer communication / its standardized version, shaped by Fielding, Berners-Lee, and others |
| JavaScript | Popular web scripting language |
| JSON | Standardized text format derived from JavaScript for structured data exchange |
6. Components
| Term | Meaning |
|---|---|
| Web component | Client, intermediary, or server that follows the uniform interface |
| Web client / browser | Program that accepts and sends representations / the common client type |
| Web server | Program that accepts and sends representations to clients |
| Web service | A server programmed with specific, often reusable, logic |
Key takeaways
- The rules aim for consistency, which clients can exploit.
- Each rule deserves consideration even if you contest it.
- The vocabulary falls into three layers: constraints (the principles), resources and messages (the data model), and components (the actors).
URIs
REST APIs address resources with URIs. Good URIs communicate the API’s resource model to developers, even though clients must still treat them as opaque identifiers, as Tim Berners-Lee’s axiom states. The chapter’s format rules are: use forward slashes for hierarchy, omit trailing slashes, use hyphens for readability, avoid underscores, prefer lowercase, and leave file extensions out of URIs. The guiding principle is that every character in a URI counts toward a resource’s identity, so URIs should be clean, precise, and predictable.
Background: why URI design matters
- Good example:
http://api.example.restapi.org/france/paris/louvre/leonardo-da-vinci/mona-lisa(the URI explains the resource model). - Bad example:
http://api.example.restapi.org/68dd0-a9d3-11e0-9f1c-0800200c9a66(opaque and hard to understand). - Berners-Lee’s axiom: an identifier is only for referring to an object. When not dereferencing, clients should not read the URI string for extra information.
- Clients must follow the Web’s linking paradigm and treat URIs as opaque, but designers should still make URIs informative for client developers.
- Generic syntax (RFC 3986):
URI = scheme "://" authority "/" path [ "?" query ] [ "#" fragment ]
Rules
| Rule | Level | Short example |
|---|---|---|
Use / to indicate hierarchy | must | /shapes/polygons/quadrilaterals/squares |
No trailing / | should not | use /shapes, not /shapes/ |
| Use hyphens for readability | should | /entries/this-is-my-first-post |
| No underscores | should not | use my-doc, not my_doc |
| Prefer lowercase in paths | should | /my-folder/my-doc |
| No file extensions | should not | use /fall, not /fall.json |
1. Forward slash separator (/) for hierarchy (must)
- The slash in the path shows a hierarchical relationship between resources.
- Example:
http://api.canvas.restapi.org/shapes/polygons/quadrilaterals/squares
2. No trailing forward slash (should not)
- A trailing slash adds no semantic value and can cause confusion.
- APIs should not expect trailing slashes and should not include them in links given to clients.
- Many frameworks treat
/shapes/and/shapesas equal, but every character counts toward identity: different URIs mean different resources. - A REST API should generate clean URIs and be intolerant of imprecise client requests.
- More forgiving APIs may redirect to the version without the trailing slash using 301 (“Moved Permanently”).
3. Hyphens ("-") for readability (should)
- Use hyphens to make long path segments easy to scan.
- Wherever English would use a space or hyphen, use a hyphen in the URI.
- Example:
http://api.example.restapi.org/blogs/mark-masse/entries/this-is-my-first-post
4. No underscores ("_") (should not)
- Viewers often underline URIs to signal they are clickable.
- Depending on the font, the underline can partially or fully hide an underscore.
- Use hyphens instead.
5. Prefer lowercase letters in paths (should)
- RFC 3986 says URIs are case-sensitive except for the scheme and host.
- Capital letters in paths can therefore cause problems.
| URI | Status |
|---|---|
http://api.example.restapi.org/my-folder/my-doc | Fine |
HTTP://API.EXAMPLE.RESTAPI.ORG/my-folder/my-doc | Identical to the first (scheme and host are case-insensitive) |
http://api.example.restapi.org/My-Folder/my-doc | Different from the first two, which may confuse |
6. No file extensions (should not)
- Do not use
.jsonor similar to indicate the format of the message body. - Rely on the media type in the
Content-Typeheader to decide how to process content. - Clients should select formats with the HTTP
Acceptrequest header (media type negotiation). - Bad:
.../transcripts/2005/fall.json - Good:
.../transcripts/2005/fall - Note: for simple links and easy debugging, an API may support media type selection through a query parameter.
Key takeaways
- Identity: every character in a URI matters, so be consistent and precise.
- Readability: hyphens and lowercase help humans, while underscores and capitals cause problems.
- Format selection: leave format to HTTP headers, not to the URI.
- Opaque but informative: clients should not parse URIs, yet good design helps developers understand the resource model.