英文:
FastAPI - Cannot use `Response` as a return type when `status_code` is set to 204
问题
以下是您要翻译的内容:
我一直在使用以下代码来处理`/healthz`的请求:
```python
@router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
summary="服务的健康检查",
description="该入口用于检查服务是否正常运行或已停止。",
# include_in_schema=False
)
def get_healthz() -> Response:
return Response(status_code=status.HTTP_204_NO_CONTENT)
这段代码自几年前以来一直在运行。
但今天我将 FastAPI 从 0.88.0 更新到 0.89.0,现在我遇到了AssertionError: 状态码 204 不应该有响应主体
的错误。完整的错误追踪如下所示:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1234, in _handle_fromlist
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "......../src/routers/healthz.py", line 20, in <module>
@router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/..../.local/share/virtualenvs/........../lib/python3.11/site-packages/fastapi/routing.py", line 633, in decorator
self.add_api_route(
File "/Users/..../.local/share/virtualenvs/......../lib/python3.11/site-packages/fastapi/routing.py", line 572, in add_api_route
route = route_class(
^^^^^^^^^^^^
File "/Users/...../.local/share/virtualenvs/....../lib/python3.11/site-packages/fastapi/routing.py", line 396, in __init__
assert is_body_allowed_for_status_code(
AssertionError: 状态码 204 不应该有响应主体
python-BaseException
我的问题是:
这是版本 0.89.0 的一个 bug 吗?还是我应该以不同的方式编写/heathz
?
即使使用return Response(status_code=status.HTTP_204_NO_CONTENT, content=None)
也会失败。
谢谢
<details>
<summary>英文:</summary>
I've been using the following code for my `/healthz`:
```python
@router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
summary="Service for 'Health Check'",
description="This entrypoint is used to check if the service is alive or dead.",
# include_in_schema=False
)
def get_healthz() -> Response:
return Response(status_code=status.HTTP_204_NO_CONTENT)
This has been working since some years ago.
Today I updated FastAPI from 0.88.0 to 0.89.0 and now I get AssertionError: Status code 204 must not have a response body
. The full tracebakc can be seen below:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1234, in _handle_fromlist
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "......../src/routers/healthz.py", line 20, in <module>
@router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/..../.local/share/virtualenvs/........../lib/python3.11/site-packages/fastapi/routing.py", line 633, in decorator
self.add_api_route(
File "/Users/..../.local/share/virtualenvs/......../lib/python3.11/site-packages/fastapi/routing.py", line 572, in add_api_route
route = route_class(
^^^^^^^^^^^^
File "/Users/...../.local/share/virtualenvs/....../lib/python3.11/site-packages/fastapi/routing.py", line 396, in __init__
assert is_body_allowed_for_status_code(
AssertionError: Status code 204 must not have a response body
python-BaseException
My question is:
Is this a bug from the version 0.89.0 , or should I write the /heathz
In a different way?
Even with return Response(status_code=status.HTTP_204_NO_CONTENT, content=None)
is failling.
Thanks
答案1
得分: 0
Bug fixed on the release 0.89.1:
https://github.com/tiangolo/fastapi/releases/tag/0.89.1
英文:
Bug fixed on the release 0.89.1:
https://github.com/tiangolo/fastapi/releases/tag/0.89.1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论