首页 > 其他分享 >Flask GET 和 POST 请求获取request 参数的方法

Flask GET 和 POST 请求获取request 参数的方法

时间:2022-11-28 17:33:51浏览次数:49  
标签:form GET Flask request Content json 参数 Type



GET  请求方式获取参数 

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

  • request.args.get('key')
  • request.values.get('key')
@app.route('/index', methods=["GET"])
def hello_world():  # put application's code here
    if request.method == "GET":
        content = request.args.get("key")
        content = request.values.get("key")

 

 

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参数

 

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

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

 

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

data = json.loads(request.get_data())

 

Content-Typeapplication/x-www-form-urlencoded  

 

 

Content-Typemultipart/form-data ,获取表单参数

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

 

标签:form,GET,Flask,request,Content,json,参数,Type
From: https://www.cnblogs.com/bigbox/p/16932801.html

相关文章