2.5 响应对象

一、设置response

设置response需要引入make_response模块

from flask import make_response
  1. 设置请求头

     headers = {
         # json格式 application/json  html格式:text/html
         'content-type': 'text/plain'
     }
    
     response.headers = headers
  2. 设置返回内容以及状态码

     response = make_response('内容', 200)

    备注:可以单独设置请求内容,不设置

二、快速定义返回内容格式

直接在函数中的返回方法上做文章

return '内容', 200, headers

备注:直接按照这种方式返回

Last updated

Was this helpful?