Understanding the tasks performed by each HTTP method
In the preceding table, the GET
HTTP verb appears twice but with two different scopes. The first row shows a GET
HTTP verb applied to a collection of games (collection of resources) and the second row shows a GET
HTTP verb applied to a game (a single resource).
Let's consider that http://localhost:8000/games/
is the URL for the collection of games. If we add a number and a slash (/
) to the preceding URL, we identify a specific game whose id or primary key is equal to the specified numeric value. For example, http://localhost:8000/games/12/
identifies the game whose id or primary key is equal to 12
.
We have to compose and send an HTTP request with the following HTTP verb (POST
) and request URL (http://localhost:8000/games/
) to create a new game. In addition, we have to provide the JSON (JavaScript Object Notation) key-value pairs with the field names and the values to create the new game. As a result of the request, the server will validate the provided values for the fields, make sure that it is a valid game and persist it in the database.
The server will insert a new row with the new game in the appropriate table and it will return a 201 Created
status code and a JSON body with the recently added game serialized to JSON, including the assigned id or primary key that was automatically generated by the database and assigned to the game object.
POST http://localhost:8000/games/
We have to compose and send an HTTP request with the following HTTP verb (GET
) and request URL (http://localhost:8000/games/{id}/
) to retrieve the game whose id or primary key matches the specified numeric value in the place where {id}
is written.
For example, if we use the request URL http://localhost:8000/games/50/
, the server will retrieve the game whose id or primary key matches 50
.
As a result of the request, the server will retrieve a game with the specified id or primary key from the database and create the appropriate game object in Python. If a game is found, the server will serialize the game object into JSON and return a 200 OK
status code and a JSON body with the serialized game object. If no game matches the specified id or primary key, the server will return just a 404 Not Found
status:
GET http://localhost:8000/games/{id}/
We have to compose and send an HTTP request with the following HTTP verb (PUT
) and request URL (http://localhost:8000/games/{id}/
) to retrieve the game whose id or primary key matches the specified numeric value in the place where {id}
is written and replace it with a game created with the provided data. In addition, we have to provide the JSON key-value pairs with the field names and the values to create the new game that will replace the existing one. As a result of the request, the server will validate the provided values for the fields, make sure that it is a valid game and replace the one that matches the specified id or primary key with the new one in the database. The id or primary key for the game will be the same after the update operation. The server will update the existing row in the appropriate table and it will return a 200 OK
status code and a JSON body with the recently updated game serialized to JSON. If we don't provide all the necessary data for the new game, the server will return a 400 Bad Request
status code. If the server doesn't find a game with the specified id, the server will return just a 404 Not Found
status.
PUT http://localhost:8000/games/{id}/
We have to compose and send an HTTP request with the following HTTP verb (DELETE
) and request URL (http://localhost:8000/games/{id}/
) to remove the game whose id or primary key matches the specified numeric value in the place where {id}
is written. For example, if we use the request URL http://localhost:8000/games/20/
, the server will delete the game whose id or primary key matches 20
. As a result of the request, the server will retrieve a game with the specified id or primary key from the database and create the appropriate game object in Python. If a game is found, the server will request the ORM to delete the game row associated with this game object and the server will return a 204 No Content
status code. If no game matches the specified id or primary key, the server will return just a 404 Not Found
status.
DELETE http://localhost:8000/games/{id}/