Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Reserve HTTP 201 for actual resource creation
Return 201 Created only when a new resource is created; use 200 OK for updates or appends to an existing resource.
Bad example
| 1 | // Appending an item to an existing collection |
| 2 | const response = await service.addItemToCollection(payload); |
| 3 | return res.status(201).json(response); // 201 implies a new resource was created |
Explanation (EN)
201 signals that a new resource was created at a new URL. Appending to an existing collection is an update, so 201 misleads clients and caches.
Objašnjenje (HR)
201 oznacava da je stvoren novi resurs na novom URL-u. Dodavanje u postojecu kolekciju je azuriranje, pa 201 zavarava klijente i cacheve.
Good example
| 1 | // Appending an item to an existing collection |
| 2 | const response = await service.addItemToCollection(payload); |
| 3 | return res.status(200).json(response); // 200 OK for updating an existing resource |
Explanation (EN)
200 OK correctly describes appending to an existing resource. 201 is reserved for endpoints that genuinely create a new resource.
Objašnjenje (HR)
200 OK ispravno opisuje dodavanje u postojeci resurs. 201 je rezerviran za endpointe koji stvarno stvaraju novi resurs.
Exceptions / Tradeoffs (EN)
Use 201 when the endpoint genuinely creates a brand-new resource, ideally with a Location header pointing to it.
Iznimke / Tradeoffi (HR)
Koristi 201 kad endpoint stvarno stvara potpuno novi resurs, idealno s Location headerom koji pokazuje na njega.