Updated:
An URL should be for specifying the ‘resources’ that the API is to manage.
The resource is ‘animals’ (cats/dogs/…) and they are uniquely identified by type/color/weight/name.
Before going into URL design, need to define the ‘business’ operations; otherwise, how the URL is designed has no relevant.
Assume you will need the following business operations
- A) Create an animal
- B) Get info of an animal
- C) Get info of all or subset of animals
- D) Update an animal
id still can be used (generated) if that provide benefit; otherwise, it can be kept at the server side and not exposed to the client. Either way it still works..
A) Create an animal
POST: /api/v1/animals/dogs/black/12/jack/
RETURN: id: <id>
OR
POST: /api/v1/animals/dogs
BODY: {‘color’ : ‘black’, ‘name’ : ‘jack’, ‘weight’ : 12 }
RETURN: id: <id>
B) Get info of an animal
GET: /api/v1/animals/<id>/
OR
GET: /api/v1/animals/dogs/black/12/jack/
C) Get info of all or subset of animals
GET: /api/v1/animals/dogs/
OR
GET: /api/v1/animals/dogs/black/
D) Update an animal
PUT: /api/v1/animals/<id>
BODY: {‘name’ : ‘jackjunior’}
OR
PUT: /api/v1/animals/dogs/black/12/jack/
BODY: {‘name’ : ‘jackjunior’}
Happy designing!