FastAPI – 当 `status_code` 设置为 204 时无法将 `Response` 用作返回类型

huangapple go评论78阅读模式
英文:

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

这里是问题截图:
FastAPI – 当 `status_code` 设置为 204 时无法将 `Response` 用作返回类型

我的问题是:

这是版本 0.89.0 的一个 bug 吗?还是我应该以不同的方式编写/heathz

即使使用return Response(status_code=status.HTTP_204_NO_CONTENT, content=None)也会失败。

0.89.0 的更改日志:
FastAPI – 当 `status_code` 设置为 204 时无法将 `Response` 用作返回类型

谢谢


<details>
<summary>英文:</summary>

I&#39;ve been using the following code for my `/healthz`:

```python
@router.get(&quot;/healthz&quot;, status_code=status.HTTP_204_NO_CONTENT, tags=[&quot;healthz&quot;],
            summary=&quot;Service for &#39;Health Check&#39;&quot;,
            description=&quot;This entrypoint is used to check if the service is alive or dead.&quot;,
            # include_in_schema=False
            )
def get_healthz() -&gt; 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1234, in _handle_fromlist
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
  File &quot;......../src/routers/healthz.py&quot;, line 20, in &lt;module&gt;
    @router.get(&quot;/healthz&quot;, status_code=status.HTTP_204_NO_CONTENT, tags=[&quot;healthz&quot;],
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/..../.local/share/virtualenvs/........../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 633, in decorator
    self.add_api_route(
  File &quot;/Users/..../.local/share/virtualenvs/......../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 572, in add_api_route
    route = route_class(
            ^^^^^^^^^^^^
  File &quot;/Users/...../.local/share/virtualenvs/....../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 396, in __init__
    assert is_body_allowed_for_status_code(
AssertionError: Status code 204 must not have a response body
python-BaseException

Here:
FastAPI – 当 `status_code` 设置为 204 时无法将 `Response` 用作返回类型

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.

Changelog of 0.89.0:
FastAPI – 当 `status_code` 设置为 204 时无法将 `Response` 用作返回类型

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

huangapple
  • 本文由 发表于 2023年1月9日 18:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定