【发布时间】:2020-04-20 22:40:33
【问题描述】:
一切运行良好,API(flask) 很好地返回数据。但是,当我尝试自定义响应代码时,我做不到。
以下是我尝试过的两种方法:
from flask import make_response
dictionary = {1:'a', 2:'b'}
resp = make_response(dictionary,1001)
return resp
#In developer tools, I could see the data, but status-code is 200
.
from flask import Response
dictionary = {1:'a', 2:'b'}
resp = Response(dictionary, status = 1001)
return resp
#Here again, I see the data, but the status code is 200
如何将状态码设置为别的?
【问题讨论】:
-
试试:
return Response("{1:'a', 2:'b'}", status=201, mimetype='application/json').Btw,1001不是有效的 HTTP 状态码 -
这里是 HTTP 状态代码 en.wikipedia.org/wiki/List_of_HTTP_status_codes 的参考。
-
@T.MLuan Thaks 感谢您的回复,先生。如果我想设置自定义状态码,我该怎么做?就像我想根据返回类型触发不同的操作一样。
标签: python flask http-status-codes