一、请求参数的方法
1、url自带的查询参数(不常用)
http://127.0.0.1:5000/test?username=woniu&password=12345
@app.route('/test')
def test():
#查询参数使用args
username = request.args.get('username')
password = request.args.get('password')
return '你的用户名:%s,你的密码:%s' %(username,password)
演示结果
2、通过Flask自定义的路由规则和参数(GET请求,代替1)
http://127.0.0.1:8000/article/123
#路由游地址参数,路由地址参数必须同步定义在接口函数里面,作为该参数的形参
@app.route('/article/<articleid>')
def article(articleid):
return '你正在访问编号为:{articleid}的文章'
演示结果
3、直接读取POST请求正文参数
@app.route('/test',methods = ['POST'])
def test():
#POST DELETE PUT 正文用form
username = request.form.get('username')
password = request.form.get('password')
return '你的用户名:%s,你的密码:%s' %(username,password)
演示结果