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

相关推荐
这里有鱼汤10 分钟前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩10 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在15 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP17 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i1 天前
python中类的基本结构、特殊属性于MRO理解
python
liwulin05061 天前
【ESP32-CAM】HELLO WORLD
python
Doris_20231 天前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_20231 天前
Python 模式匹配match case
前端·后端·python
这里有鱼汤1 天前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员