I am reading on RESTful API, and this confused me.
DELETE /zoos/12/animals/5 - Deletes animal #5 for zoo #12
Why do anyone need to imply what zoo it is when you know the animal id? Won't just DELETE /animals/5
do the job?
Or is this statement means to delete the 5th animal for zoo #12?
Using the following request, I will explain this RESTful request:
DELETE /zoos/12/animals/5
The DELETE
, defines the method for the REST Request. In this case, we are requesting that we remove/delete a resource.
The /zoos/12/animals/5
is the resource that we are making the request on. You can think of this as a Unique ID, but not for a single entity in a database. It is a logical entity that may span multiple tables in a traditional relational database.
This specific REST URI suggests that we are selecting a Zoo from Zoos with ID 12 and an Animal from Animals with ID 5, although REST doesn't mean that the backend implementation is Relational.
However, using a NoSQL Db, this URI could mean that we are accessing Zoo Document 12. This document would then likely have a 'animals' array property, which then may iterpret the the animal as the 5th in the array.
The purpose of REST is to abstract the traditional approach of Relation Database and restrict users from requesting Resources that are out of scope or which they are not authorized to access.
I like to think of it as walking through a building. When you are in a given room, you can only access rooms that are adjacent to one and other and that have a door available to them. If there is no door (or window, staircase, ladder,etc) in the current room to move to a different resource, then you wouldn't be able to access those rooms without finding a different entry place and path.