Skip to content

Intro to Flask restful

tsungtwu edited this page Apr 19, 2017 · 1 revision

Intro to Flask Restful

homepage: https://flask-restful.readthedocs.io/en/latest/

Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up.

or

Flask-RESTPlus http://flask-restplus.readthedocs.io/en/latest/index.html

Integrate with Flask-Restful and Swagger

Restful tutorial

http://seanlin.logdown.com/posts/247554-introduction-to-rest

https://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful

restplus : http://michal.karzynski.pl/blog/2016/06/19/building-beautiful-restful-apis-using-flask-swagger-ui-flask-restplus/

好處:RequestParser

Request Argument


parser = reqparse.RequestParser()
parser.add_argument('rate', type=int, help='Rate cannot be converted')
parser.add_argument('name')
args = parser.parse_args()

Note
The default argument type is a unicode string. This will be str in python3 and unicode in python2.***

Argument Locations

By default, the RequestParser tries to parse values from flask.Request.values, and flask.Request.json.

Use the location argument to add_argument() to specify alternate locations to pull the values from. Any variable on the flask.Request can be used. For example:

 # Look only in the POST body
parser.add_argument('name', type=int, location='form')

# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')

# From the request headers
parser.add_argument('User-Agent', location='headers')

# From http cookies
parser.add_argument('session_id', location='cookies')

# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')

Restful api design

HTTP Method URI Action
GET http://[hostname]/todo/api/v1.0/tasks Retrieve list of tasks
GET http://[hostname]/todo/api/v1.0/tasks/[task_id] Retrieve a task
POST http://[hostname]/todo/api/v1.0/tasks Create a new task
PUT http://[hostname]/todo/api/v1.0/tasks/[task_id] Update an existing task
DELETE http://[hostname]/todo/api/v1.0/tasks/[task_id] Delete a task

Refence

Clone this wiki locally