Flask-RESTPlus Has What You Need
Learn how to make a REST API server with the help of the Flask-RESTPlus package
We'll cover the following
Flask and HTTP methods
Flask is a micro-framework, and we can use it to create a REST API by connecting routes and specifying HTTP methods (for example, GET
, POST
), parsing input and output JSON strings, and handling errors. An easier approach is to use the batteries-included package flask-restplus
.
Flask-RESTPlus
If you want to work on it on your local system, you can install it using pipenv
in your project directory.
pipenv install flask-restplus flask-cors
Here, we’ve also installed flask-cors
, which will make it easier later to develop the front-end and back-end at the same time.
Using Flask-RESTPlus classes
Let’s start with a small example of a Flask app that uses the Resource
and API classes provided by Flask-RESTPlus. This example defines an API on the route /my-api
and is used to track the favorite numbers listed in the code below. It provides a GET
method to get the list of all numbers in the list, and a POST
method to record a new favorite number. Each item must have its own unique ID so that it can be accessed directly. In this case, they’ll be accessed through /my-api/<id>
.
Get hands-on with 1200+ tech skills courses.