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

相关推荐
寻星探路6 分钟前
测试开发话题10---自动化测试常用函数(2)
java·前端·python
鸢尾掠地平12 分钟前
Python中常用内置函数上【含代码理解】
开发语言·python
萧鼎18 分钟前
Python 图像处理利器:Pillow 深度详解与实战应用
图像处理·python·pillow
高洁0118 分钟前
大模型-详解 Vision Transformer (ViT)
人工智能·python·深度学习·算法·transformer
执笔论英雄1 小时前
【大模型推理】sglang 源码学习设计模式: 策略和访问者
python·学习·设计模式
Geo_V1 小时前
Windows 安装 Anaconda 并配置 PyCharm 环境
ide·windows·python·pycharm
hnxaoli1 小时前
win10程序(十四)pdf转docx简易版
开发语言·python·pdf
CodeCraft Studio1 小时前
PDF处理控件Aspose.PDF教程:在Python中向PDF文档添加页面
开发语言·python·pdf
测试老哥1 小时前
Jmeter吞吐量控制器详解
自动化测试·软件测试·python·测试工具·jmeter·测试用例·压力测试
吗喽1号1 小时前
python-xmind转Excel
python·测试工具