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

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

FastAPI - Cannot use `Response` as a return type when `status_code` is set to 204

问题

以下是您要翻译的内容:

  1. 我一直在使用以下代码来处理`/healthz`的请求
  2. ```python
  3. @router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
  4. summary="服务的健康检查",
  5. description="该入口用于检查服务是否正常运行或已停止。",
  6. # include_in_schema=False
  7. )
  8. def get_healthz() -> Response:
  9. return Response(status_code=status.HTTP_204_NO_CONTENT)

这段代码自几年前以来一直在运行。

但今天我将 FastAPI 从 0.88.0 更新到 0.89.0,现在我遇到了AssertionError: 状态码 204 不应该有响应主体的错误。完整的错误追踪如下所示:

  1. Traceback (most recent call last):
  2. File "<frozen importlib._bootstrap>", line 1234, in _handle_fromlist
  3. File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  4. File "......../src/routers/healthz.py", line 20, in <module>
  5. @router.get("/healthz", status_code=status.HTTP_204_NO_CONTENT, tags=["healthz"],
  6. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  7. File "/Users/..../.local/share/virtualenvs/........../lib/python3.11/site-packages/fastapi/routing.py", line 633, in decorator
  8. self.add_api_route(
  9. File "/Users/..../.local/share/virtualenvs/......../lib/python3.11/site-packages/fastapi/routing.py", line 572, in add_api_route
  10. route = route_class(
  11. ^^^^^^^^^^^^
  12. File "/Users/...../.local/share/virtualenvs/....../lib/python3.11/site-packages/fastapi/routing.py", line 396, in __init__
  13. assert is_body_allowed_for_status_code(
  14. AssertionError: 状态码 204 不应该有响应主体
  15. 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` 用作返回类型

谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;ve been using the following code for my `/healthz`:
  4. ```python
  5. @router.get(&quot;/healthz&quot;, status_code=status.HTTP_204_NO_CONTENT, tags=[&quot;healthz&quot;],
  6. summary=&quot;Service for &#39;Health Check&#39;&quot;,
  7. description=&quot;This entrypoint is used to check if the service is alive or dead.&quot;,
  8. # include_in_schema=False
  9. )
  10. def get_healthz() -&gt; Response:
  11. 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:

  1. Traceback (most recent call last):
  2. File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1234, in _handle_fromlist
  3. File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
  4. File &quot;......../src/routers/healthz.py&quot;, line 20, in &lt;module&gt;
  5. @router.get(&quot;/healthz&quot;, status_code=status.HTTP_204_NO_CONTENT, tags=[&quot;healthz&quot;],
  6. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  7. File &quot;/Users/..../.local/share/virtualenvs/........../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 633, in decorator
  8. self.add_api_route(
  9. File &quot;/Users/..../.local/share/virtualenvs/......../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 572, in add_api_route
  10. route = route_class(
  11. ^^^^^^^^^^^^
  12. File &quot;/Users/...../.local/share/virtualenvs/....../lib/python3.11/site-packages/fastapi/routing.py&quot;, line 396, in __init__
  13. assert is_body_allowed_for_status_code(
  14. AssertionError: Status code 204 must not have a response body
  15. 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:

确定