get请求参数是在URL里面的,post请求参数是放在请求头里面的
@index_page.route("/get")
def get():
var_a = request.args.get("a", "jarvis")
return "request:%s,params:%s,var_a:%s" %(request.method,request.args,var_a)
浏览器访问 http://127.0.0.1:5000/get
自定义参数值,浏览器访问 http://127.0.0.1:5000/get?a=123
@index_page.route("/post", methods=["POST"])
def post():
var_a = request.form['a']
return "request:%s,params:%s,var_a:%s" % (request.method, request.form, var_a)
postman测试
使用reques.values同时获取get和post请求的参数:
@index_page.route("/get")
def get():
req = request.values
var_a = req['a'] if "a" in req else ""
return "request:%s,params:%s,var_a:%s" %(request.method,request.args,var_a)
@index_page.route("/post", methods=["POST"])
def post():
req = request.values
var_a = req['a'] if "a" in req else ""
return "request:%s,params:%s,var_a:%s" % (request.method, request.form, var_a)
这样的好处就是不用在代码里面区分get和post请求是用args和form来获取