This code is a self explanatory code, I got from here.
The current repository helps in getting started with the basic REST API Development in Flask. The sole existence of this repo is for a Powercoders lecture. The application uses chinook.db that students used in the SQL workshop.
- a computer with Python installed
- virtual environment (which is a copy of the Python interpreter onto which you can install packages privately, without affecting the global Python interpreter installed in your system)
- on Python 3:
python3 -m venv virtual-environment-name
- on Python 2:
sudo pip install virtualenv
- to start using a virtual environment, you have to “activate” it:
source venv/bin/activate
- Clone this repository.
git clone https://github.com/anuschka/python-rest-flask.git
- cd into python-rest-flask
- Install the requirements
pip install -r requirements.txt
- Setup environment variables
FLASK_APP=app.py
FLASK_DEBUG=True
- Run Flask
flask run
There are three endpoints in the application:
- http://127.0.0.1:5002/employees shows ids of all the employees in database
- http://127.0.0.1:5002/tracks shows tracks details
- http://127.0.0.1:5002/employees/8 shows details of employee whose employee ID is 8
To create a new record in the Employees table:
curl -H "Content-Type: application/json" -X POST -d '{"LastName":"Power","FirstName":"Coder","Title":"Developer","ReportsTo":"Matthias","BirthDate":"1.1.1990","HireDate":"20.9.2017","Address":"GarageHub","City":"Zurich","Country":"Switzerland","State":"None","PostalCode":"8100","Phone":"0123456789","Fax":"0123456789","Email":"coder@powercoders.com"}' http://127.0.0.1:5000/employees
To delete Employee whose employee ID is 8:
curl -H "Content-Type: application/json" -X DELETE -v http://127.0.0.1:5000/employees/8