引言
今天同事在一个flask项目写业务的时候碰到一个问题,在写一个get接口时,一直报400,不知道什么原因。后面我帮忙一起看了下。
分析
刚开始我也是一脸懵逼,因为我这边是没报错,他那边报错,后来仔细看了一下报错信息,是request.json发出来的错误,我很好奇的看了一下源码。
@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()
request.json原来是调用的get_json(),我们接下来看get_ json()
if cache and self._cached_json[silent] is not Ellipsis:
return self._cached_json[silent]
if not (force or self.is_json):
return None
data = self._get_data_for_json(cache=cache)
try:
rv = json.loads(da