1、简要说明

近日,在使用flask框架获取前端的请求时获取参数时,遇到了几个问题;之前的项目也有使用这部分,当时程序没有问题就没再深究,直到遇到了问题。果然,遇到问题才会成长!^_^

因此,对GET和POST两种请求方式的参数获取方式进行梳理。

request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的一些信息。客户端可通过HTML表单或在网页地址后面提供参数的方法提交数据,然后通过request对象的相关方法来获取这些数据。

request请求总体分为两类:

get请求
GET把参数包含在URL中,访问时会在地址栏直接显示参数不安全,且参数大小比较小

post请求
参数通过request body传递

2、常见的方式

在最初使用时,上网一搜,得到的结果大致如下:

flask获取参数方式:

request.form.get("key", type=str, default=None) # 获取表单数据
request.args.get("key") # 获取get请求参数
request.values.get("key") # 获取所有参数

上述是三种方式,可以满足基本的使用,但是并不是万能的!

3、GET请求方式获取参数

当采用GET请求方式时,参数直接显示在请求连接中,可以使用两种获取参数的方式:

request.args.get('key')
request.values.get('key')

在route装饰器语句中,通过methods指定请求方式,如下:

@app.route("/", methods=["GET"])

获取参数

if request.method == "GET":
    comment = request.args.get("content")
    comment = request.values.get("content")

4、POST请求方式获取参数

客户端在发送post请求时,数据可以使用不同的Content-Type 来发送。

比如:

以 application/json 的方式 ,请求body体的内容就是{“a”: “b”, “c”: “d”}
以 application/x-www-form-urlencoded 的方式,则body体的内容就是 a=b&c=d
在Postman软件中,可以方便的查看参数是以什么形式发送的,对应的Content-Type是什么。

Body中选择“raw”,则对应的Headers中的“Content-Type”是“application/json”,参数形式是{“content”:”很好”}

Body中选择“x-www-form-urlencoded”,则对应的Headers中的“Content-Type”是“application/x-www-form-urlencoded”,参数形式是Key-Value形式。

Body中选择“form-data”, 则对应的Headers中的“Content-Type”是“multipart/form-data”,参数形式是Key-Value。

具体位置如下图:

POST请求不同Content-Type的处理方式
Content-Type为 application/json,获取json参数

request.get_json()['content']
# 或者
request.json.get('centent')

获取的是序列化后的参数,一般情况下满足使用,不需要json.loads()来序列化。
打印出结果就是json串,如{‘name’:’lucy’, ‘age’:22}

Content-Type为 application/json,获取json原始参数

request.get_data()

request.get_data()获取的原始参数,接受的是type是’bytes’的对象,如:b{‘name’:’lucy’, ‘age’:22}

Content-Type为application/x-www-form-urlencoded

request.values.get('key')

Content-Type为multipart/form-data ,获取表单参数

可以使用request.form.get(‘content’) 或者 request.form[‘content’]来获取参数

request.form.get('key')
# 或者
request.form['key']

代码示例

if request.method == "POST":
     if request.content_type.startswith('application/json'):            
         # comment = request.get_json()["content"]
         comment = request.json.get('content')
     elif request.content_type.startswith('multipart/form-data'):
         comment = request.form.get('content')
     else:
         comment = request.values.get("content")

5、完整示例

@app.route("/", methods=["GET", "POST"])
def process():
    if request.method == "GET":
        comment = request.args.get("content")
        # comment = request.values.get("content")
    if request.method == "POST":
        if request.content_type.startswith('application/json'):            
            # comment = request.get_json()["content"]
            comment = request.json.get('content')
        elif request.content_type.startswith('multipart/form-data'):
            comment = request.form.get('content')
        else:
            comment = request.values.get("content")
    logger.debug('%s'%comment)