flask获取请求对象的get和post参数

前言

get请求参数是在URL里面的,post请求参数是放在请求头里面的

get请求:

python 复制代码
@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


post请求

python 复制代码
@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请求的参数:

python 复制代码
@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来获取

思考:如果一个post请求里面有有共同的参数,这个时候会打印出什么呢?

验证:

可以看到打印出了get请求的参数值,GET优先级高于POST

相关推荐
总有刁民想爱朕ha42 分钟前
Python自动化从入门到实战(24)如何高效的备份mysql数据库,数据备份datadir目录直接复制可行吗?一篇给小白的完全指南
数据库·python·自动化·mysql数据库备份
孤客网络科技工作室1 小时前
Python - 100天从新手到大师:第五十七天获取网络资源及解析HTML页面
开发语言·python·html
帅大大的架构之路1 小时前
高级篇:Python脚本(101-150)
开发语言·python
liweiweili1261 小时前
Django中处理多数据库场景
数据库·python·django
reasonsummer2 小时前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
E_ICEBLUE2 小时前
高效压缩 PDF 文件大小(3 大实用的 Python 库)
python·pdf
栀寒老醑3 小时前
Python实现的服务器日志监控脚本
开发语言·python
yaoxin5211234 小时前
211. Java 异常 - Java 异常机制总结
java·开发语言·python
Empty_7776 小时前
编程之python基础
开发语言·python
哲Zheᗜe༘9 小时前
了解学习Python编程之python基础
开发语言·python·学习