问题:在烧瓶中返回HTTP状态代码201
我们正在使用Flask作为我们的API之一,我只是想知道是否有人知道如何返回HTTP响应201?
对于诸如404之类的错误,我们可以调用:
from flask import abort
abort(404)
但是对于201我得到
LookupError:201也不exceptions
我是否需要创建自己的exceptions,像这样的文档?
We’re using Flask for one of our API’s and I was just wondering if anyone knew how to return a HTTP response 201?
For errors such as 404 we can call:
from flask import abort
abort(404)
But for 201 I get
LookupError: no exception for 201
Do I need to create my own exception like this in the docs?
回答 0
您可以在这里阅读。
return render_template('page.html'), 201
You can read about it here.
return render_template('page.html'), 201
回答 1
您可以使用Response返回任何http状态代码。
> from flask import Response
> return Response("{'a':'b'}", status=201, mimetype='application/json')
You can use Response to return any http status code.
> from flask import Response
> return Response("{'a':'b'}", status=201, mimetype='application/json')
回答 2
As lacks suggested send status code in return statement
and if you are storing it in some variable like
notfound = 404
invalid = 403
ok = 200
and using
return xyz, notfound
than time make sure its type is int not str. as I faced this small issue
also here is list of status code followed globally
http://www.w3.org/Protocols/HTTP/HTRESP.html
Hope it helps.
回答 3
你可以做
result = {'a': 'b'}
return jsonify(result), 201
如果您想在响应中返回JSON数据以及错误代码,请在此处和此处阅读有关响应的信息,以获取make_response API详细信息
You can do
result = {'a': 'b'}
return jsonify(result), 201
if you want to return a JSON data in the response along with the error code
You can read about responses here and here for make_response API details
回答 4
理想情况下,在烧瓶代码中,还应尽可能频繁地指定MIME类型:
return html_page_str, 200, {'ContentType':'text/html'}
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
…等等
In your flask code, you should ideally specify the MIME type as often as possible, as well:
return html_page_str, 200, {'ContentType':'text/html'}
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
…etc
回答 5
you can also use flask_api for sending response
from flask_api import status
@app.route('/your-api/')
def empty_view(self):
content = {'your content here'}
return content, status.HTTP_201_CREATED
you can find reference here http://www.flaskapi.org/api-guide/status-codes/
回答 6
根据API的创建方式,通常返回201(创建),您将返回创建的资源。例如,如果它正在创建一个用户帐户,您将执行以下操作:
return {"data": {"username": "test","id":"fdsf345"}}, 201
请注意,后缀数字是返回的状态码。
或者,您可能希望向客户端发送一条消息,例如:
return {"msg": "Created Successfully"}, 201
Dependent on how the API is created, normally with a 201 (created) you would return the resource which was created. For example if it was creating a user account you would do something like:
return {"data": {"username": "test","id":"fdsf345"}}, 201
Note the postfixed number is the status code returned.
Alternatively, you may want to send a message to the client such as:
return {"msg": "Created Successfully"}, 201
回答 7
就我而言,我必须将上述内容组合在一起才能使其正常工作
return Response(json.dumps({'Error': 'Error in payload'}),
status=422,
mimetype="application/json")
In my case I had to combine the above in order to make it work
return Response(json.dumps({'Error': 'Error in payload'}),
status=422,
mimetype="application/json")
回答 8
因此,如果您使用flask_restful
API的Package ,则返回201会像
def bla(*args, **kwargs):
...
return data, 201
哪里data
应该是任何hashable / JsonSerialiable值,例如dict,string。
So, if you are using flask_restful
Package for API’s
returning 201 would becomes like
def bla(*args, **kwargs):
...
return data, 201
where data
should be any hashable/ JsonSerialiable value, like dict, string.