0
点赞
收藏
分享

微信扫一扫

在 Flask 中,当你不确定客户端发送的是哪种 HTTP 请求时,你可以使用 ​​request.args.get​​ 方法来获取所有参数。这个方法适用于 GET 请求,它允许你获取 URL 中的查

在 Flask 中,request 对象用于处理 HTTP 请求。关于请求参数的获取,request 对象提供了几个不同的属性,例如 argsformdatajson。让我们来详细了解一下这些属性之间的区别和使用场景:

  1. request.args:
  • args 属性用于获取请求路径中的查询参数(即 URL 中问号后的部分)。
  • 例如,对于路径 /hello?name=zsargs 解析出来的数据类似于一个字典,其值为 {"name": "zs"}
  • 使用场景:适用于获取 GET 请求中的查询参数。
  1. request.form:
  • form 属性用于处理表单数据。
  • 当客户端通过 POST 请求提交表单数据时,这些数据会被包含在请求的正文中,并由 Flask 解析成 request.form 对象。
  • form 返回一个类似字典的 ImmutableMultiDict 对象,可以通过键名来获取对应的值。
  • 使用场景:适用于处理 POST 请求中的表单数据,特别是当请求头的 content-typeapplication/x-www-form-urlencodedmultipart/form-data 时。
  1. request.data:
  • data 属性是请求体的原始数据,以字节形式表示。
  • 适用于处理非表单数据的情况,例如自定义的请求体格式。
  1. request.json:
  • json 属性用于获取 JSON 数据。
  • 当请求头的 content-typeapplication/json 时,Flask 会将请求体中的 JSON 数据解析成一个 Python 字典。
  • 使用场景:适用于处理包含 JSON 数据的请求。
  1. request.values:
  • values 属性是一个合并了 argsform 的结果。
  • 它返回一个 CombinedMultiDict 对象,包含了 GET 请求参数和 POST 请求表单数据的合并结果。
  • 使用场景:适用于获取所有请求参数,无论是 GET 请求的查询参数还是 POST 请求的表单数据¹²³⁴.

推荐使用 request.values.get() 来获取参数,因为它会同时考虑 GET 请求和 POST 请求的数据。如果你只关心某一种类型的数据,可以使用对应的属性来获取²。

源: 与必应的对话, 2024/4/19

(1) 理解 flask中request中form、data、json、values属性的区别 - 知乎. https://zhuanlan.zhihu.com/p/551703472.

(2) flask中获取request的参数的方法 - 年轻人——001 - 博客园. https://www.cnblogs.com/chaojiyingxiong/p/10283877.html.

(3) Flask request.form 与 request.values.get() 区别-详细内容 - 黄兵的个人博客. https://pdf-lib.org/Home/Details/13211.

(4) flask中request模块中form,args,value,files等属性 - CSDN博客. https://blog.csdn.net/yoyolearing/article/details/99568920.

(5) 关于Flask中的request方法和数据,一定要明白 - 知乎. https://zhuanlan.zhihu.com/p/623755314.





在 GET 请求中,参数直接显示在请求的 URL 中。我们可以使用以下两种方式来获取参数:

  • 使用 request.args.get('key'):获取 GET 请求参数。
  • 使用 request.values.get('key'):获取所有参数(包括 GET 请求和其他方式传递的参数)。


在 Flask 中,当你不确定客户端发送的是哪种 HTTP 请求时,你可以使用 request.args.get 方法来获取所有参数。这个方法适用于 GET 请求,它允许你获取 URL 中的查询参数。

以下是一些关于参数传递和请求方式的总结:

  1. 参数设置
  • 参数类型
  • string:默认的参数类型,如果没有指定参数类型,会默认为参数是字符串类型。
  • int:指定参数为整数类型。
  • float:指定参数为浮点数类型。
  • path:获取当前路径。
  • uuid:获取 UUID 类型参数。
  • 示例

# 未指定参数类型,默认为 string 类型
@app.route('/<id>/')
def h(id):
    id = int(id) ** 5
    id = str(id)
    return id

# 指定参数类型为 int
@app.route('/<int:id>/')
def h(id):
    id = id ** 5
    id = str(id)
    return id

# 指定参数类型为 path
@app.route('/<path:url_path>/')
def h(url_path):
    return 'path: %s' % url_path

# 指定参数类型为 uuid
@app.route('/<uuid:uu>/')
def h(uu):
    return 'uu: %s' % uu

  1. 请求方式设置
  • 默认情况下,Flask 请求是 GET 请求。如果想要指定其他请求方式,可以使用 methods 参数。
  • 示例:

# 指定 POST 请求方式
@app.route('/register/', methods=['POST'])
def register():
    register_dict = request.form
    username = register_dict['username']
    password = register_dict.get('password')

    # 处理用户数据并存入数据库
    # ...

    return '创建用户成功'

  1. 全局变量上下文引用
  • 使用 g 对象来存储全局变量,例如:

from flask import g

@app.route('/search', methods=['GET', 'POST'])
def fsearch():
    if request.method == 'POST':
        g.results = multiselect(request)  # 处理表单数据
    return 'done'

@app.route('/result', methods=['GET', 'POST'])
def fresult():
    results = g.results
    # 渲染结果页面
    return render_template("result.html")

  1. 传参示例
  • Cookies 传参

# 获取 cookie
@app.route('/get_cookie')
def get_cookie():
    name = request.cookies.get('passwd')
    return name

# 删除 cookie
@app.route('/del_cookie')
def del_cookie():
    resp = make_response('delete_cookie')
    resp.delete_cookie('passwd')
    return resp

# 设置 cookie
@app.route('/set_cookie')
def set_cookie():
    resp = make_response('set_cookie')
    resp.set_cookie('passwd', '123456')
    return resp

  • 静态模板中传参

<!-- test.html -->
<a href="{{ url_for('test', name=1) }}">点击这里查看</a>

@app.route('/test/<name>', methods=['GET'])
def test(name):
    print(name)
    return render_template('/test.html')

  • URL 中传参

@app.route('/tk', methods=['POST', 'GET'])
def tk():
    p = request.args.get('p')
    type = request.args.get('type')
    print(p)
    print(type)

@app.route('/tk', methods=['POST', 'GET'])

def tk():

   p = request.args.get('p')

   type = request.args.get('type')

   print(p)

   print(type)¹: Flask中路由参数传递、请求方式小结 - 知乎

²: Flask 如何使用 Flask 请求传递 GET 参数 - 极客教程

³: [Flask Flask request.args.get 获取所有参数(Python) - 极客教程](https://geek-docs.com/flask/flask

源: 与必应的对话, 2024/4/19

(1) Flask中路由参数传递、请求方式小结 - 知乎 - 知乎专栏. https://zhuanlan.zhihu.com/p/79847375.

(2) Flask 如何使用 Flask 请求传递 GET 参数 - 极客教程. https://geek-docs.com/flask/flask-questions/448_flask_how_to_pass_get_parameters_to_url_using_flask_request.html.

(3) Flask Flask request.args.get 获取所有参数(Python) - 极客教程. https://geek-docs.com/flask/flask-questions/268_flask_flask_requestargsget_get_all_params_python.html.

(4) 获取flask请求参数的几种方式_flask获取请求参数-CSDN博客. https://blog.csdn.net/DH2442897094/article/details/115955818.

(5) flask 获取GET和POST请求参数(全) - CSDN博客. https://blog.csdn.net/ling620/article/details/107562294

.

举报

相关推荐

0 条评论