方法或属性 | 描述 |
---|---|
headers | 类似于字典,用来存放headers |
status | A string with a response status. |
status_code | The response status as integer. |
data | A descriptor that calls get_data() and set_data(). |
get_json(force=False, silent=False, cache=True) | Parse data as JSON. |
is_json | Check if the mimetype indicates JSON data, either application/json or application/*+json. |
max_cookie_size | Read-only view of the MAX_COOKIE_SIZE config key. |
mimetype | The mimetype (content type without charset etc.) |
set_cookie(key, value=’’, max_age=None, expires=None, path=’/’, domain=None, secure=False, httponly=False, samesite=None) | 设置cookie |
from flask import Flask, json, make_response
app = Flask(__name__)
@app.route('/1')
def hello1():
return 'Hello'#当只有一个字符串返回,会自动转换为状态码为200, MIME 类型是text/html的response对象
@app.route('/2')
def hello11():
test={'key1':'value1','key2':'value2'}
return json.dumps(test)#返回json格式文件
@app.route('/3')
def hello2():
return 'Hello3',200,{"key":"value"}#当返回多个字段时,会智能对照,MIME 类型是text/html的response对象,字典里的东西会被当成headers
@app.route('/4')
def hello3():
rsp = make_response('hello4') #这个方法生成了一个response对象
rsp.mimetype = 'text/plain'
rsp.headers['key'] = 'value'
rsp.set_cookie('user','wang')#这个值可以用接下来访问的request.cookies来取得
return rsp #使用make_response来处理response
if __name__ == '__main__':
app.run(debug=True)
参考文献:
http://www.jianshu.com/p/0ced03604e24