[Flask] Restful simple application interface testing

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
# @author: shenqiang 

from Flask Import Flask, make_response, jsonify
 from flask_restful Import   the Resource, of Api, reqparse 

'' ' instantiate this class Flask '' ' 
App = the Flask ( the __name__ ) 

' '' calls the Restful Flask '' ' 
API = of Api (= App App) 

' '' arranged index router '' ' 
@ app.route ( ' / index ' )
 DEF index ():
    return jsonify({'status':0,'msg':'success','datas':{'userid':1003,'name':'shenqiang','age':'18'}})

'''配置login路由器'''
class LoginView(Resource):
    def get(self):
        return jsonify({'status':0,'msg':'Success ' , ' DATAS ' : {}}) 

    DEF POST (Self):
         '' ' simple data and parity constraints '' ' 
        Parser = reqparse.RequestParser () 
        parser.add_argument ( ' username ' , type = STR, Help = ' your user parameters can not be empty ' , required = True) 
        parser.add_argument ( ' password ' , of the type = str) 
        parser.add_argument ( ' Age ' , of the type = int, Help = ' your age must be an integer ')
        returnjsonify ({ ' Status ' : 0, ' MSG ' : ' Success ' , ' Data ' : parser.parse_args {()}}) 

'' ' add request address: routing address comprising API, view ' '' 
api.add_resource (the LoginView, ' / Login ' , Endpoint = ' Login ' ) 

IF  the __name__ == ' __main__ ' : 
    app.run (Debug = True)

Write simple API

Guess you like

Origin www.cnblogs.com/shen-qiang/p/12028442.html