request模块

1、request.method

查看请求方式

2、request.form

获取POST请求提交的数据

1
2
3
4
5
6
@app.route('/login',methods=['GET','POST'])
def fn():
if request.method == 'POST': # 判断用户请求是否是post请求
print(request.form) # ImmutableMultiDict([('user_name', '吕星辰'), ('user_password', '123456')])
print(request.form.get('user_name')) # 吕星辰
print(request.form.to_dict()) # {'user_name': '吕星辰', 'user_password': '123456'}

上边代码,模拟了前端表单 post请求,在flask中,对于post请求,获取数据使用form,获取到的是一个对象,可使用**get方法**获取对应的数据(如果没有数据,get方法会返回None,但是用其他获取单个数据方式会报错!!!),也可以使用**to_dict( )**方法转化为字典。

3、request.args

获取GET请求提交的数据

1
2
3
4
5
@app.route('/login',methods=['GET','POST'])
def fn():
print(request.args) # ImmutableMultiDict([('user_name', '吕星辰'), ('user_password', '123')])
print(request.args.get('user_name')) # 吕星辰
print(request.args.to_dict()) # {'user_name': '吕星辰', 'user_password': '123'}

**上边代码,同上,模拟前端表单get请求,在flask中,对于get请求,获取数据使用**args**,获取到的是一个对象,也可使用**get方法**获取对应的数据,也可以使用*to_dict( )*方法转化为字典。

4、request.path

获取域名后面的url路径

1
2
# 请求的是  http://0.0.0.0:9527/req?id=10
print(request.path) # '/req'

5、request.base_url

获取路径包含根路径

1
2
#请求 http://0.0.0.0:9527/req?id=10
print(request.base_url) # 'http://0.0.0.0:9527/req'

6、request.headers

获取请求头信息,请求headers信息,dict形式

1
2
3
4
5
6
7
8
9
10
11
12
request.headers.get("Accept")
'''
Accept: text/html, application/xhtml+xml, */*
Content-Type: application/x-www-form-urlencoded
Accept-Language: zh-CN
Accept-Encoding: gzip, deflate
Host: localhost:5000
Content-Length: 55
Connection: Keep-Alive
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like GeckoCore/1.70.3704.400 QQBrowser/10.4.3587.400
'''

7、request.host

获取主机域名地址

8、request.host_url

获取根路径,包含斜线

1
2
# 请求路径为 http://0.0.0.0:9527/req?id=10
print(request.host) # http://0.0.0.0:9527/

9、request.remote_addr

访问的远程IP地址

10、request.files

文件上传

11、request.json和request.get_json

request.get_json(force=False, silent=False, cache=True)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
##源码
@property
def json(self):
"""This will contain the parsed JSON data if the mimetype indicates
JSON (:mimetype:`application/json`, see :meth:`is_json`), otherwise it
will be ``None``.
"""
return self.get_json()

def _get_data_for_json(self, cache):
return self.get_data(cache=cache)

def get_json(self, force=False, silent=False, cache=True):
"""Parse and return the data as JSON. If the mimetype does not
indicate JSON (:mimetype:`application/json`, see
:meth:`is_json`), this returns ``None`` unless ``force`` is
true. If parsing fails, :meth:`on_json_loading_failed` is called
and its return value is used as the return value.
:param force: Ignore the mimetype and always try to parse JSON.
:param silent: Silence parsing errors and return ``None``
instead.
:param cache: Store the parsed JSON to return for subsequent
calls.
"""

request.json()调用的是request.get_json()方法,request.get_json()方法默认情况下只对minmetype为==appication/json== 的请求可以正确解析。并将数据作为json输出。如果minmetype不是json格式(appication/json),则返回None。请求头content-type 为 application /json ,所以要使用get_json( ) 方法去获取数据,使用request.form 无法获取 用时axios post请求的数据!