Resources, collections and nesting: how to read a REST address
A REST address names a resource with a noun and the HTTP method sets the action. A collection is plural, an item is addressed by id, and nesting shows ownership, as in /users/42/orders. Filters, sorting and pagination go in the query. The address implies checks: a missing id gives 404, an empty collection 200 with [], and a nested address must not expose others' data. Naming style is checked against the contract.
API · Client-server and REST
One endpoint says /getUserOrders?id=42, another /users/42/orders. Which one is REST?
The documentation has two endpoints with a similar meaning: GET /getUserOrders?id=42 and GET /users/42/orders. Both work, and an interview asks you to assess the address. A REST API address is not random: you read from it what the resource is, whose collection it is and what the action is. The checks come from there too: what to expect for a non-existent id, whether a nested address opens someone else's data. This article is about how to read an address and what to verify from it.
The logic of the address. The path names a resource (a noun), and the HTTP method sets the action. A plural name is a collection, an address with an identifier is one item. Why: the method already says "what to do" (GET reads, DELETE removes), so a verb in the path duplicates it, and for clients and caches a resource gets one stable address that can be opened, cached and saved.
GET /orders list of orders
POST /orders create an order
GET /orders/17 one order
PATCH /orders/17 partial update
DELETE /orders/17 delete
GET /users/42/orders orders of user 42
Nesting (/users/42/orders) shows that an order belongs to a user. People usually do not go deeper than two levels: when an order has its own id, /orders/17 is enough. Filters, sorting and pagination are query parameters: /orders?status=paid&sort=-createdAt&page=2. The path identifies a resource, the query narrows or orders the selection. Actions that do not fit CRUD (cancel, confirm) are modeled as a state change (PATCH with a new status) or an action subresource: POST /orders/17/cancel. A verb in the path is an acceptable compromise as long as the action is called with POST, not GET: GET must be safe and change nothing.
Assessing an address
GET /getUserOrders?id=42
A verb in the name and the id in the query instead of the path. Every endpoint is named in its own way, so the next one cannot be guessed.
GET /users/42/orders
Nouns, a plural collection, the owner in the path. This scheme lets you guess /users/42/orders/17.
Two situations that are easy to confuse. "There is nothing" can mean two different results, and bugs hide exactly here:
404 or 200 with an empty list
The resource does not exist
GET /orders/999999 and GET /users/999999/orders: there is no such order or user. Expect 404.
The collection is empty
GET /users/42/orders for a user who has no orders yet. Expect 200 and [].
Example: a nested address that ignores the owner. Order 17 belongs to user 43. A test as user 42 requests it through "their own" address:
GET /api/users/42/orders/17 HTTP/1.1
Authorization: Bearer eyJ… (token of user 42)
HTTP/1.1 200 OK
{"id":17,"userId":43,"total":1250,"status":"paid"}
How to read it. The response has userId: 43, the address says 42. The server checked that order 17 exists but did not compare its owner with the parent resource in the path (or with the token's owner). The user sees someone else's data: this is a broken object-level authorization. The right answer is 404 (do not reveal that the order exists) or 403. The autotest:
test('order of another user is not reachable', async ({ request }) => {
// order 17 belongs to user 43, user 42 is asking
for (const path of ['/api/users/42/orders/17', '/api/orders/17']) {
const res = await request.get(path, {
headers: { Authorization: 'Bearer ' + token42 },
});
expect([403, 404]).toContain(res.status());
}
});
Both addresses are checked because protection is often added to one route only, and the other stays open.
What else a tester checks.
- Consistency: one naming style (plural, case, hyphens) across all endpoints.
- A trailing slash and letter case (
/orders/,/Orders) behave the same on all endpoints: a redirect or a 404, but not different things. - Collection pagination: the default limit, the maximum limit, a page beyond the data.
- An id in a wrong format (
/orders/abc) gives400or404, but not500. - A filter with an unknown value (
?status=unknown): an empty selection or a validation error, as the contract says.
Limits. "RESTful" is not a dogma. The action POST /orders/17/cancel breaks the pure scheme but is often clearer than the alternative, and a tester compares behavior with the contract, not with taste. Naming rules should follow the project's style: older APIs have singular names, camelCase or verbs, and changing addresses breaks clients. So such remarks go as a recommendation, not a bug, if the behavior matches the documentation.
Common mistakes.
- Expecting
404for an empty collection: then the client shows an error instead of "no orders yet". - Checking that a nested resource exists but not that it belongs to the parent: this gives access to other people's data through any id.
- Testing only the "happy" address and not trying a wrong id format, extra segments or letter case.
- Filing a naming style as a bug when it matches the documentation.
Check yourself
GET /users/42/orders returns 404 for a user with no orders. Why can this be a bug?
Answer
User 42 exists, their collection is just empty: expect 200 and []. A 404 tells the client the resource is missing, and the interface shows an error instead of an empty state. But if user 42 does not exist, 404 is right.
Why is it not enough, for /users/42/orders/17, to check that order 17 exists?
Answer
You also check that it belongs to user 42 and that the token belongs to 42. If the server looks only at the order id, changing the number in the address lets you read other people's orders.
/getUserOrders?id=42 there is a verb and the id in the query; GET /users/42/orders is better. Middle/Senior: From the address I derive checks: a non-existent id gives 404, an empty collection 200 with [], a nested address does not return other people's data, filters live in the query, the collection has pagination. I also remind that "RESTful" is not a dogma: a tester compares behavior with the contract, and naming style is only a recommendation.