from flask import make_response
@app.route('/hello')
def hello():
# status code 200, 404, 301
# content-type http headers
# content-type: text/html
# Response
headers = {
'content-type': 'application/json',
'location': 'http:www.baidu.com',
}
response = make_response('<html><html>', 301)
response.headers = headers
return response
flask带有路由的函数,本质上返回的是一个response
,这也是为什么我们需要make_response函数,其中包含了:
- 状态码
code
, 比如200,404,301。 - header,其中最重要的是content-type, 也就是返回的数据类型,默认是text/html。如果需要是json,就可以改为application/json.