gpt4 book ai didi

python - Flask - 使用 DELETE 得到 500 个错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:15 27 4
gpt4 key购买 nike

我确定我遗漏了一些明显的东西,但它是:

@app.route(PREFIX + '/<table>/<int:index>', methods = ['DELETE'])
def delete_row(table, index=None):
if table not in can_delete:
abort(404)
try:
del database[table][index]
except:
abort(404)
print('returning')

运行结果如下:

 * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
returning
127.0.0.1 - - [13/Feb/2015 11:28:44] "DELETE /api/json/types/ig-folders/1 HTTP/1.1" 500 -
127.0.0.1 - - [13/Feb/2015 11:29:01] "DELETE /api/json/types/ig-folders/99 HTTP/1.1" 404 -
127.0.0.1 - - [13/Feb/2015 11:28:35] "DELETE /api/json/types/invalid/1 HTTP/1.1" 404 -

我的 GET 处理程序工作正常,DELETE 处理程序工作正常,除了讨厌的 500 错误。问题是我没有返回任何值吗?我的印象是 DELETE 应该只返回状态,而不是数据。

最佳答案

删除成功后不返回响应。抛出 500 是因为 None 不是 Flask View 的有效返回值。

返回an acceptable response type ,就像一个字符串,而不是。

对于 200 响应,您可以返回:

'Success', 200, {'Content-Type': 'text/plain'}

或者您可以返回 204 No Content ,您将在其中为响应正文返回一个空字符串:

'', 204

关于python - Flask - 使用 DELETE 得到 500 个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com