【问题标题】:What is the difference between returning jsonify() and returning make_response from in flask rest API?从烧瓶休息 API 返回 jsonify() 和返回 make_response 有什么区别?
【发布时间】:2019-10-31 07:06:13
【问题描述】:

post方法的return语句有什么区别?

class User(Resource):
     def post(self):
       #some functionality code
        return jsonify({'messsage':'Success','status_code':200})

class Login(Resource):
    def post(self):
       #some functionality code
        return make_response({'messsage':'Success','status_code':200})

class Resgister(Resource):
    def post(self):
       #some functionality code
        return {'messsage':'Success'},200

【问题讨论】:

    标签: python python-3.x flask


    【解决方案1】:

    jsonify 使用 application/json mimetype 的给定参数的 JSON 表示创建一个响应。 而 ma​​ke_response 用于设置额外的标头或可用于转换响应对象中的值。

    更多信息请阅读:jsonify & make_response

    希望对您有所帮助! :)

    【讨论】:

      【解决方案2】:

      使用jsonify(),您将能够序列化您的返回数据,然后从Flask 返回一个Response 对象,尽管{'messsage':'Success'},200 也将在内部被Flask 转换为Response 对象。使用make_response(),您将能够将视图(html 页面或模板)渲染为Response 对象,并且能够设置标题、状态代码等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-29
        • 2019-05-06
        • 2011-12-24
        • 2022-10-18
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多