返回HTTP状态代码201瓶
问题描述:
我们正在使用的瓶为我们的API之一,我只是想知道是否有人知道如何返回HTTP响应201?返回HTTP状态代码201瓶
错误,如404,我们可以称之为:
from flask import abort
abort(404)
但对于201,我得到
LookupError: no exception for 201
我需要创建我自己的异常喜欢在文档this?
答
由于缺乏建议发送状态代码return语句 ,如果你是在一些变量存储它像
notfound = 404
invalid = 403
ok = 200
,并使用
return xyz, notfound
不是一定要确保它的类型是int不是str。因为我面临这个小问题 也这里是全球各地的状态码列表 http://www.w3.org/Protocols/HTTP/HTRESP.html
希望它有帮助。
答
在你烧瓶代码,您应尽可能多地指定MIME类型,以及:
回报html_page_str,200,{ '的ContentType': 'text/html的'}
返回JSON .dumps({ '成功':真}),200,{ '的ContentType': '应用/ JSON'}
...等
答
您可以使用响应返回任何HTTP状态代码。
> from flask import Response
> return Response("{'a':'b'}", status=201, mimetype='application/json')
'回报',201' – Luc