Flask设置响应信息的方法

2020-09-16  本文已影响0人  测试探索
返回的响应数据
from flask import Flask,request,abort,Response,make_response

app = Flask(__name__)

@app.route("/index")
def index():
    # 1.使用元组,返回自定义的响应信息
    #       响应体     状态码 响应头
    # return "index page",400,[("itcast","python"),("City","shenzhen")]
    # return "index page", 400, {"itcast1": "python1", "City1":"shenzhen1"}
    # 666 itcast status--666状态码与后面必须空格分隔
    return "index page", "666 itcast status", {"itcast1": "python1", "City1":"shenzhen1"}

if __name__ == '__main__':
    app.run(debug=True)
运行结果
from flask import Flask,request,abort,Response,make_response

app = Flask(__name__)

@app.route("/index")
def index():
    # 2.使用make_response,来构造响应信息
    resp = make_response("index page 2")
    resp.status = "999 itcast"   #设置状态码
    resp.headers["city"] = "sz"  #设置响应头
    return resp


if __name__ == '__main__':
    app.run(debug=True)
image.png
上一篇 下一篇

猜你喜欢

热点阅读