我们正在使用Flask为我们的API之一,我只是想知道是否有人知道如何返回HTTP响应201?
对于404这样的错误,我们可以调用:
from flask import abort
abort(404)
但是201美元我得到了
LookupError: 201没有例外
我需要在文档中创建自己的异常吗?
我们正在使用Flask为我们的API之一,我只是想知道是否有人知道如何返回HTTP响应201?
对于404这样的错误,我们可以调用:
from flask import abort
abort(404)
但是201美元我得到了
LookupError: 201没有例外
我需要在文档中创建自己的异常吗?
当前回答
你可以在这里阅读。
return render_template('page.html'), 201
其他回答
如果你在API中使用flask_restful Package 返回201就会变成
def bla(*args, **kwargs):
...
return data, 201
其中data应该是任何可哈希/ JsonSerialiable值,如dict, string。
你可以在这里阅读。
return render_template('page.html'), 201
返回语句中缺少建议的发送状态代码 如果你把它存储在某个变量中
notfound = 404
invalid = 403
ok = 200
和使用
return xyz, notfound
当我遇到这个小问题时,请确保它的类型是int而不是str 此外,这里是状态代码的列表遵循全局 http://www.w3.org/Protocols/HTTP/HTRESP.html
希望能有所帮助。
在你的flask代码中,理想情况下,你应该尽可能多地指定MIME类型,以及:
return html_page_str, 200, {'ContentType':'text/html'}
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
等
您可以使用Response来返回任何http状态代码。
> from flask import Response
> return Response("{'a':'b'}", status=201, mimetype='application/json')