This tutorial helps setup flask restful api’s.
Install Python Packages:
Open cmd and navigate into your testApp folder and run the following commands.
pip install flask-RESTful && pip freeze > requirements.txt
__init__.py:
On the init of your application you will need to setup flask_restful. There are config options you could set for config.py. Look into it!
from flask_restful import Api api = Api(app) #Add api endpoints #Get api.add_resource(home.views.MyResource, '/home/getMyData/') #Post api.add_resource(home.views.MyResource, '/home/getMyData/', methods=['POST'])
Setup home views.py:
You need to import Resource in it’s most simplistic form. However if you want to deal with request parameters add in reqparse and inputs. Inputs give you access to boolean that way a boolean can be parsed into a python boolean easily.
from flask_restful import Resource, reqparse, inputs
You can now use get, post, etc. I will give you three examples below.
Get:
class MyResource(Resource): def get(self): return {}
Get /w Parameter:
class MyResource(Resource): def get(self, var): return {}
Get /w Parameter & Request Parameter:
class MyResource(Resource): def get(self, var): parser = reqparse.RequestParser() parser.add_argument('new_request_var', type=str, default='') #If you want to have a boolean request parameter do the following. parser.add_argument('new_request_var_bool', type=inputs.boolean, default=False) args = parser.parse_args(strict=True) new_request_var = args['new_request_var'] new_request_var_bool = args['new_request_var_bool'] return {}
Post:
class MyResource(Resource): def post(self): return {}