首页 > 解决方案 > flask_restful 响应的默认 mimetype(内容类型)

问题描述

对不起,也许这是一个有点愚蠢的问题,但我无法在文档或库的源代码中找到它flask_restful。flask_restful 响应的默认内容类型是什么?

在我项目的所有库中,我发现以下内容:

- flask: default_mimetype = "text/html"
- werkzeug.sansio: 
      default_mimetype = "text/plain"
      mimetype = get_content_type(mimetype, self.charset)
- werkzeug.utils: mimetype = application/octet-stream

从该行mimetype = get_content_type(mimetype, self.charset)看起来它可能依赖于内容,但我正在返回一个字典:

class FooResource(Resource):
    def get(self):
        return {'hello': 'world'}, 200

我得到的内容类型是application/json. 有人可以解释这种行为吗?application/json即使没有在任何地方指定,我怎么可能获得内容类型?如果我需要确定内容类型是application/json,明确返回它会更好吗?(我知道我可以在请求后使用)

class FooResource(Resource):
    def get(self):
        return {'hello': 'world'}, 200, {'Content-Type': 'application/json'}

非常感谢。

标签: flask-restfulresponse-headers

解决方案


推荐阅读