2👍
Generally, right REST API for each resource in most cases consists of 2 endpoints:
- List/Create endpoint
- Details (Retrieve/Update/Destroy) endpoint.
POST-request is used in the first one and is used for item creation.
More detailed on the endpoints, described above. URLs for the endpoints usually are almost the same, except of ID which is present in the second endpoint. For example:
/api/items/
— List/Create endpoint, with GET-request it will return list of items, with POST-request it will create a new item and return it serialized.
/api/items/<item_id>/
— Details endpoint, which allows us to deal with some item, identified by <item_id>
. GET-request returns the item, PUT request updates the whole item (all required fields should be present in payload, if some non-required field is omitted — it’s value will be set to None
), PATCH-request allows to update item partially, only the provided in payload fields will be updated, all other fields will stay untouched) and DELETE request, obviously, deletes the item.
Such approach applies some limitations sometimes, but if you follow it — your code will stay slim and clear, because a lot of things will be done in generic way. Both on back-end and front-end parts of your project.