Anatomy of a REST API
determine what the actual resource looks like.
What a "lion" object looks like?
{
"name": "Simba",
"id": 1,
"age": 3,
"pride": "the cool cats",
"gender": "male"
}
Always model your data first!It also helps you to mock this out for other developers.
Then design the routes to access the resources.
GET, POST, PUT, DELETE
get: I'm asking for something
post: I'm giivin you someting
put: I'm updating somethin
delete: destroy something
CRUD: create, read, update, delete on your data
diagram with JSON how our routes will look like
{
"GET /lions": {
"desc": "returns all lions",
"response": "200 application/json",
"data": [{}, {}, {}]
},
"GET /lions/:id": {
"desc": "returns one lion respresented by its id",
"response": "200 application/json",
"data": {}
},
"POST /lions": {
"desc": "create and returns a new lion uisng the posted object as the lion",
"response": "201 application/json",
"data": {}
},
"PUT /lions/:id": {
"desc": "updates and returns the matching lion with the posted update object",
"response": "200 application/json",
"data": {}
},
"DELETE /lions/:id": {
"desc": "deletes and returns the matching lion",
"response": "200 application/json",
"data": {}
}
}
"data"
is what the response should look like".../:id"
describes an element in a collection (get by it's "id")You can also use YAML for your blueprint.
POST route: you also want that the server creates AND responds back with the object it just created. Example a todo app, needs to send back the todo you just created for the UI.
- PATCH?
- doesn't have any opinion on it, he just uses PUT
You can also do sorts with REST:
http://example.com/lions?sort=age