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

相关推荐
axinawang23 分钟前
第3课:变量与输入
python
idingzhi1 小时前
A股量化策略日报()
python
zyk_computer1 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
weixin199701080161 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
萌新小码农‍1 小时前
python装饰器
开发语言·前端·python
KK溜了溜了1 小时前
Python从入门到精通
服务器·开发语言·python
2401_884454151 小时前
mysql处理复杂SQL性能_InnoDB优化器与MyISAM差异
jvm·数据库·python
m0_470857642 小时前
golang如何实现目录大小统计_golang目录大小统计实现方案
jvm·数据库·python
消晨消晨2 小时前
MONAI初上手——模型构建
pytorch·python·monai
weixin_444012932 小时前
如何在多实例管理时隐藏MySQL版本信息_安全混淆与配置
jvm·数据库·python