【Flask】Flask Restful api

### Install:

Flask-Restful requires Flask 0.8 or later to run on Python 2.6 or Python 3.3. It can be installed by pip install flask-restful.

### Basic usage:
1. Import `Api` from `flask_restful` to create an `api` object.
2. Write a view function, let it inherit from `Resource`, and then use the request method you want to define the corresponding method in this. For example, if you want this view to only use `post` request, then Define a `post` method.
3. Use `api.add_resource` to add views and `url`.
The sample code is as follows:

1 class LoginView(Resource):
2     def post(self,username=None):
3         return {"username":"saber"}
4 
5 api.add_resource(LoginView,'/login/<username>/', endpoint='login')
6  

 

Notes:
* If you want to return json data, then use flask_restful, if you want to render the template, then use the previous method, which is the `app.route` method.
* The url is the same as before, you can pass parameters. Also different from the previous one, you can specify multiple urls.
* endpoint is used to specify when url_for reverses the url. If you do not write endpoint, the lowercase view name will be used as the endpoint.


### Parameter verification:
The Flask-Restful plugin provides a package similar to WTForms to verify whether the submitted data is legal, called reqparse. Here is the basic usage:

1  class LoginView(Resource):
 2      def post(self):
 3          parser = reqparse.RequestParser()
 4          parser.add_argument( ' username ' , type=str, help=u ' username verification error ' , default= ' abc ' , required= True)
 5          parser.add_argument( ' password ' , type=str, help=u ' password error ' , default= ' 123456 ' )
 6          parser.add_argument( ' age' , type=int, help=u ' age error ' , default= 0)
 7          parser.add_argument( ' gender ' , type=str, choices=[ ' male ' , ' female ' , ' secret ' ], help=u ' Gender error ' )
 8          parser.add_argument( ' homepage ' , type=inputs.url, help=u ' home page connection error ' )
 9          parser.add_argument( ' phone ', type=inputs.regex(r ' 1[3578]\d{9} ' ), help=u ' mobile phone number is wrong ' )
 10          parser.add_argument( ' birthday ' , type=inputs.date, help=u ' date Validation error ' )
 11          args = parser.parse_args()
 12          print args
 13          return { " username " : ' saber ' }
 14  
15 api.add_resource(LoginView, ' /login/ ' , endpoint= 'login')

 


add_argument can specify the name of this field, the data type of this field, etc. Some parameters of this method will be explained in detail below:
1. default: default value, if this parameter has no value, then the value specified by this parameter will be used.
2. required: whether it is required. The default is False, if set to True, then this parameter must be submitted. 3. type: The data type of this parameter. If specified, the specified data type will be used to coerce the submitted value.
4. choices: options. The submitted value only meets the value in this option to pass the verification, otherwise the verification fails.
5. help: error message. If validation fails, the value specified by this parameter will be used as the error message.
6. trim: Whether to remove the leading and trailing spaces.

Among them, you can use some data types that come with python, or you can use some specific data types under flask_restful.inputs to force conversion. For example, some commonly used ones:
1. url: it will judge whether the value of this parameter is a url, if not, an exception will be thrown.
2. regex: Regular expression.
3. date: Convert this string to datetime.date data type. If the conversion is unsuccessful, an exception will be thrown.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250270&siteId=291194637