flask 关于get、post的写法

一、

get 方法获取入参方法  request.args.get

地址: 127.0.0.1:5000/test?arg1=1&arg2=2

如图:

二、

pots方法获取入参方法类型比较多,地址 127.0.0.1:5000/test

1.request.get_json,入参json格式 {"arg1":1,"arg2":"2"} 如图

2.request.入参form格式,如图:

代码如下:

 1 from flask import Flask
 2 app = Flask(__name__)
 3 
 4 @app.route('/xxxx',methods=['GET','POST'])
 5 def xxxx():
 6     if request.method == 'POST':
 7         #入参 json格式
 8         argsJson = request.get_json()
 9         arg1= argsJson['arg1']
10         arg2= argsJson['arg2']
11         '''
12         #入参form格式
13         arg1= request.form['arg1']
14         arg2= request.form['arg2']'''
15     else:
16         arg1= request.args.get('arg1')
17         arg2= request.args.get('arg2')
18     #调用 test方法
19     res = test(arg1,arg2)
20     return res
21 
22 if __name__ == '__main__':
23     app.run(host='0.0.0.0')        

猜你喜欢

转载自www.cnblogs.com/whycai/p/11827996.html
今日推荐