Meaning
This chunk defines a REST API endpoint pattern for replacing the state of a specific subresource that belongs to a parent resource. It addresses the need to modify nested data without flattening the resource hierarchy or losing ownership context. Triggered when designing or consuming APIs where child entities are logically owned by a parent and updates must target a single nested item.
Primary Function
API design
Communicative Purpose
Enables targeted, idempotent updates to nested resources while preserving the parent-child relationship in the URL hierarchy.
Pattern
PUT /{parent_resource}/{parent_id}/{subresource}
Core Structure
PUT /{...}/{...}/{...}
Função primária
API design
Propósito comunicativo
Enables targeted, idempotent updates to nested resources while preserving the parent-child relationship in the URL hierarchy.
Situações de gatilho
REST API design: updating a single item within a collection owned by a parent resource; Microservices: modifying a related entity without exposing the parent's full collection; CRUD operations: replacing the state of a specific nested record
Contextos
RESTful APIs, web services, microservices, HTTP-based backends, OpenAPI specifications
Padrão
PUT /{parent_resource}/{parent_id}/{subresource}
Estrutura central
PUT /{...}/{...}/{...}
Slots de substituição
parent_resource: string (plural noun for the owning collection), parent_id: identifier type (int, UUID, string), subresource: string (plural noun for the child collection or singular item identifier)
Colocados típicos
- HTTP status codes (200
- 204
- 404)
- JSON request body
- Content-Type application/json header
- authentication middleware
- path parameter validation
Substituições comuns
- PATCH /resource/{id}/subresource — partial update instead of full replacement
- PUT /subresource/{id} — flattened hierarchy loses parent context
- POST /resource/{id}/subresource/{sub_id} — non-idempotent action verb for creation
Erros comuns
Using POST instead of PUT — POST is non-idempotent and signals creation, causing duplicate side effects on retry; Omitting the parent ID — flattens the hierarchy and creates ambiguity when subresource IDs collide across parents; Returning 200 with the full updated resource vs 204 No Content — inconsistent with client expectations and caching behavior; Not validating that the subresource belongs to the parent — allows cross-parent data leakage; Using singular nouns in the path — breaks REST collection conventions
Similar / contraste
PUT /resource/{id} — updates the parent itself, not a child; DELETE /resource/{id}/subresource/{sub_id} — removes rather than replaces; PATCH /resource/{id}/subresource — partial update of the subresource
Interferências
Coming from RPC/SOAP: may treat the endpoint as an action-oriented method rather than a resource-oriented noun phrase — REST treats the URL as the address of the thing being modified, not the operation name. Coming from GraphQL: may try to express nested updates through a single mutation with complex input types — REST uses distinct URL paths per resource level.
Família do chunk
- REST resource nesting
- HTTP verb semantics
- idempotent updates
- subresource CRUD
- URL hierarchy design
Nuance
When NOT to use: avoid deep nesting beyond two levels (e.g., /a/{id}/b/{id}/c/{id}) as it becomes unreadable and suggests the data model needs flattening. Performance: each PUT replaces the entire subresource representation, so large payloads increase bandwidth — use PATCH for partial updates. Boundary conditions: PUT must be idempotent — repeated identical requests must yield the same server state; if the subresource does not exist, some APIs return 404 while others upsert with 201.
Efeito pragmático
Provides a predictable, cacheable, and idempotent URL contract for updating nested data, enabling reliable client retries and clear API documentation.
Dica de memória
PUT to a nested address is like mailing a corrected letter to a specific apartment in a building — the building (parent) and apartment (subresource) together pinpoint exactly what gets updated.
Upgrade path
PUT /resource/{id}/subresource/{sub_id}/action — adding sub-resource actions, or transitioning to PATCH for partial updates and GraphQL mutations for complex nested writes
Log in to save chunks.