"Flask" Flask Restful API

Source: Internet
Author: User
Tags flask restful

# # Installation:

Flask-restful needs to run on Python2.6 or Python3.3 in the Flask 0.8 or more versions. Install with PIP install flask-restful.

# # # Basic use:
1. Import ' API ' from ' flask_restful ' to create an ' API ' object.
2. Write a view function, let him inherit from ' Resource ', then in this, use the way you want to define the appropriate method, such as you want to use this view only the ' post ' request, then define a ' post ' method.
3. Use ' Api.add_resource ' to add views and ' URLs '.
The sample code is as follows:

1 classLoginView (Resource):2     defPost (self,username=None):3         return{"username":"Saber"}4 5Api.add_resource (LoginView,'/login/<username>/', endpoint='Login')6  

Precautions:
* If you want to return JSON data, then use flask_restful, if you want to render the template, then the previous way, that is, ' App.route ' way.
* The URL is still the same as before, you can pass parameters. It's not the same as before, you can specify multiple URLs.
* Endpoint is used to give url_for the URL to reverse the time specified. If you do not write endpoint, the lowercase of the name of the view will be used as the endpoint.


# # # Parameter Validation:
The Flask-restful plugin provides a package similar to Wtforms to verify that the submitted data is legitimate, called Reqparse. Here is the basic usage:

1 classLoginView (Resource):2     defpost (self):3Parser =Reqparse. Requestparser ()4Parser.add_argument ('username', Type=str, Help=u'User name validation error', default='ABC', required=True)5Parser.add_argument ('Password', Type=str, Help=u'Password Error', default='123456')6Parser.add_argument (' Age', Type=int, Help=u'Age Error', default=0)7Parser.add_argument ('Gender', Type=str, choices=['male','female','Secret'], Help=u'Gender Errors')8Parser.add_argument ('Homepage', Type=inputs.url, Help=u'Home page Connection error')9Parser.add_argument ('Phone', Type=inputs.regex (R'1[3578]\d{9}'), Help=u'Phone number Error')TenParser.add_argument ('Birthday', Type=inputs.date, Help=u'Date validation Error') Oneargs =Parser.parse_args () A         Printargs -         return{"username":'Saber'} -  theApi.add_resource (LoginView,'/login/', endpoint='Login')


Add_argument can specify the name of this field, the data type of the field, and so on. Some of the parameters of this method are explained in detail below:
1. Default: Defaults, if this parameter has no value, then the value specified by this parameter will be used.
2. Required: Whether it is necessary. The default is False, and if set to True, then this parameter must be submitted. 3. Type: The data type of this parameter, if specified, then the value of the commit will be cast using the specified data type.
4. Choices: Option. The value submitted comes up only if the value in this option meets the validation pass, otherwise the validation does not pass.
5. Help: Error message. If the validation fails, the value specified by this parameter will be used as the error message.
6. Trim: Whether to remove the space before and after.

The type, which can use some of the data types that Python comes with, can also be cast using some specific data types under Flask_restful.inputs. For example, some commonly used:
1. URL: Will determine whether the value of this parameter is a URL, if not, then throw an exception.
2. Regex: Regular expression.
3. Date: Converts this string to the Datetime.date data type. If the conversion is unsuccessful, an exception is thrown.

"Flask" Flask Restful API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.