Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building RESTful Python Web Services

You're reading from  Building RESTful Python Web Services

Product type Book
Published in Oct 2016
Publisher Packt
ISBN-13 9781786462251
Pages 418 pages
Edition 1st Edition
Languages
Author (1):
Gaston C. Hillar Gaston C. Hillar
Profile icon Gaston C. Hillar

Table of Contents (18) Chapters

Building RESTful Python Web Services
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
1. Developing RESTful APIs with Django 2. Working with Class-Based Views and Hyperlinked APIs in Django 3. Improving and Adding Authentication to an API With Django 4. Throttling, Filtering, Testing, and Deploying an API with Django 5. Developing RESTful APIs with Flask 6. Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask 7. Improving and Adding Authentication to an API with Flask 8. Testing and Deploying an API with Flask 9. Developing RESTful APIs with Tornado 10. Working with Asynchronous Code, Testing, and Deploying an API with Tornado 11. Exercise Answers

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}/ 
You have been reading a chapter from
Building RESTful Python Web Services
Published in: Oct 2016 Publisher: Packt ISBN-13: 9781786462251
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}