When would an API return a parameterised link such as /orders{?status,page} marked templated instead of a fully resolved URL, and what does RFC 6570 expansion require the client to do with it?
answer
- template = family of URLs the server cannot enumerate
- templated: true - braces are not a valid URL
- {?a,b} query, {/x} path, {+x} reserved, {x*} explode
- undefined variables vanish on expansion
- parameter names become a documented contract
basics
~20 sUse a template when the target is a family of URLs the server cannot enumerate - search, filter, lookup by an id the client holds. The client must expand it with an RFC 6570 implementation, supplying named variables; expansion handles encoding and drops undefined variables, and only then is the result a usable URL.
solid answer
~50 sA resolved link is right when there is one specific target - next page, self, a particular sub-resource. A template is right when the target is parameterised by the caller: /orders{?status,page,size} or /users/{id}. The server cannot pre-render every combination, so it publishes the shape instead. The marker matters. A client must not treat a templated href as a URL: braces are not valid in a request target and expansion has real rules. RFC 6570 defines operators - {?a,b} appends a form-style query string, {/x} adds a path segment, {+x} lets reserved characters through unencoded, {x*} explodes a list or map. Expansion percent-encodes values and silently omits undefined variables. The trade-off is honest: a template moves the parameter vocabulary back onto the client, which is coupling. It buys generality for search-shaped endpoints, at the cost of the client needing a template library and knowing what status means.
code
json · 8 lines{
"_links": {
"self": { "href": "/orders?page=2" },
"next": { "href": "/orders?cursor=eyJpZCI6NDJ9" },
"search": { "href": "/orders{?status,customerId,page,size}", "templated": true },
"item": { "href": "/orders/{orderId}", "templated": true }
}
}go deeper
Recognise a templated link, know it must be expanded before use, and that variables go into the braces.
Name the common operators, explain that a library handles encoding and drops undefined variables, and give a search-versus-next-page example.
Argue the coupling trade-off - variable names become a versioned contract - and say when to prefer resolved links, especially for pagination.
Decide platform-wide where templates are allowed, require documented variable vocabularies per relation, and keep templates generated from routing definitions so they cannot drift.
## Resolved link versus template A hypermedia response mostly carries links the server has already computed - self, next, the URL of a related resource. Those are ready to use. But some affordances are not a single URL: a search endpoint, a filterable collection, or a lookup by an identifier the client holds. Enumerating every combination is impossible, so the server publishes a URI Template (RFC 6570) and lets the client fill it in. Because the two look alike in JSON, formats mark templates explicitly - typically a templated: true flag next to the href. A client that ignores the flag and requests the raw string will send braces on the wire and get a 404 or a rejected request. ## The syntax, briefly A template is a string with expressions in braces. The first character of an expression may be an operator: - **{var}** simple expansion - substitutes a percent-encoded value. - **{+var}** reserved expansion - permits reserved characters like / and ? through unencoded, used when the variable holds a path fragment. - **{#var}** fragment expansion. - **{/var}** path-segment expansion - prefixes a slash. - **{?a,b}** form-style query - emits ?a=1&b=2, and emits nothing at all if no variable is defined. - **{&a}** continuation - appends &a=1 to a URL that already has a query. - **{var*}** explode modifier - expands a list or map into repeated or paired parameters. - **{var:3}** prefix modifier - truncates to the first 3 characters. RFC 6570 organises these into levels 1 to 4, level 1 being plain {var} substitution and level 4 adding modifiers. Many client libraries implement level 4 fully. ## What the client must do Use a library, not string replace. Correct expansion involves per-operator encoding rules, the difference between reserved and unreserved character sets, list and map handling, and the rule that undefined variables simply disappear - which is what makes {?status,page} degrade cleanly to a bare collection URL when the caller supplies nothing. Hand-rolled substitution gets encoding wrong the first time a value contains a space, an ampersand or a non-ASCII character. ## The coupling trade-off This is the part worth saying out loud. The promise of hypermedia is that the client depends on relation names, not on URL structure. A template partially retracts that: the client must now know that the variable is called status and that its legal values are PENDING or SHIPPED. The URL shape is still the server's business, but the parameter vocabulary becomes a shared contract that has to be documented and versioned like any other. Adding a new optional variable is safe; renaming or removing one breaks callers. That is why the usual guidance is: prefer resolved links wherever the server knows the target, and reserve templates for genuinely caller-parameterised access - search, filtering, sorting, direct lookup by an identifier the client already possesses. Pagination is the classic case for resolved links precisely because the server knows what the next page is and can hide cursors, offsets and sort state inside an opaque href. ## Practical notes - Give the template a relation name like any other link, and document the variables alongside that relation. - Do not put secrets or signed values into templates the client fills in; the server cannot sign what it did not build. - Templates and content negotiation are independent - a template describes a URL, not a representation. - Server-side helpers can build templates from route definitions, which keeps them from drifting away from the actual routing table.
- Why is pagination usually expressed with resolved next and prev links rather than a page template?Because the server knows exactly what the next page is and can encode cursors, sort state and filter state opaquely inside the href. A template forces the client to understand the paging scheme, which breaks the day you move from offsets to cursors. Resolved links let that migration happen server-side with no client change.
saying these in an interview costs you the question
- Requesting a templated href directly, braces and all
- Expanding templates with naive string replacement instead of an RFC 6570 implementation
- Assuming a missing variable produces an empty parameter rather than being omitted entirely
- Templating pagination and thereby exposing the paging scheme as a client contract