赞
踩
请求方式:POST
报错提示:
AttributeError: 'NoneType' object has no attribute 'get'
根据报错提示分析:空类型对象那个没有"get"属性
。
代码中打印 request.get_json()
函数的值,打印结果为None
,没有取到值。再使用 request.data
函数取值,打印结果为byte
类型数据。如下图:
根据以上测试数据,可以判断问题就出在 request.get_json()
函数上。查看源代码:
# 源代码 def get_json(self, force=False, silent=False, cache=True): """Parse :attr:`data` as JSON. If the mimetype does not indicate JSON (:mimetype:`application/json`, see :meth:`is_json`), this returns ``None``. 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.get_json()
函数默认情况下只对 mimetype
为 application/json
的请求可以正确解析,否则返回None
。
http
请求增加header: Content-Type:application/json
;request.get_json(force=True)
忽略mimetype
。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。