Flask request.get_json()方法报错:AttributeError: 'NoneType' object has no attribute 'get'

一、问题描述:
请求方式:POST
报错提示:

AttributeError: 'NoneType' object has no attribute 'get'

二、问题分析:
根据报错提示分析:空类型对象那个没有"get"属性。
代码中打印 request.get_json() 函数的值,打印结果为None
可以判断问题就出在 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.
    """

三、解决方法:
1.http 请求增加header: Content-Type:application/json ;
2.使用 request.get_json(force=True) 忽略mimetype。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容