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

相关推荐
cheungxiongwei.com23 分钟前
Rust 驱动的 Python 工具革命:Ruff 和 uv 与传统工具的对比分
python·rust·uv
web135085886353 小时前
Python大数据可视化:基于python的电影天堂数据可视化_django+hive
python·信息可视化·django
东方芷兰3 小时前
伯克利 CS61A 课堂笔记 11 —— Mutability
笔记·python
不会Hello World的小苗6 小时前
Java——列表(List)
java·python·list
m0_748235958 小时前
Python大数据可视化:基于Python的王者荣耀战队的数据分析系统设计与实现_flask+hadoop+spider
hadoop·python·flask
Dyan_csdn8 小时前
【Python项目】基于Python的Web漏洞挖掘系统
网络·python·安全·web安全
Minner-Scrapy8 小时前
DApp 开发入门指南
开发语言·python·web app
&小刘要学习&9 小时前
anaconda不显示jupyter了?
python·jupyter
jerry-899 小时前
jupyterhub_config配置文件内容
python
奔跑吧邓邓子9 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程