python restful风格接口 配置路由 自定义url

时间:2024-03-14 17:02:02
from flask import Flask,request,render_template
from flask_restful import reqparse, abort, Api, Resource
    
    @app.route("/login",methods=["POST","GET"])   #定义路由(路径)

def login():
    if request.method=="POST":
        username = request.form.get('username') #   获取formdata里的数据
        password = request.form.get('password')
        username = request.args.get('username') #   获取请求头里的数据(url)
        password = request.args.get('password')

        print (username)
        print (password)
        return u'POST'+'+'+username+'+'+password

    if request.method=="GET":
        print('call get now')
        username=request.args.get('username')
        password=request.args.get('password')
        print(username)
        print(password)
        return username

@app.route("/logout",methods=["POST","GET"])	#定义路由(路径)

def logout():
    if request.method=="POST":
        username=request.form.get('username')
        password=request.form.get('password')
        print (username)
        print (password)
        return u'POST'+'+'+username+'+'+password

    if request.method=="GET":
        print('call get now')
        username=request.args.get('username')
        password=request.args.get('password')
        print(username)
        print(password)
        return username


if __name__ == '__main__':
    app.run(debug=True)

方法:login的post请求:http://127.0.0.1:5000/login?username=123&password=3
python restful风格接口 配置路由 自定义url
返回的结果:
POST+123+3

方法:login的get请求:http://127.0.0.1:5000/login?username=123&password=3
返回的结果:
123

方法:logout的post请求:http://127.0.0.1:5000/logout?username=123&password=3
python restful风格接口 配置路由 自定义url
返回的结果:
POST+12+12

方法:logout的get请求:http://127.0.0.1:5000/logout?username=123&password=3
返回的结果:
123