Fastapi: Can't assign http response status code integer bigger than 600

Created on 28 Apr 2020  路  4Comments  路  Source: tiangolo/fastapi

Describe the bug

When i use Response class with param status_code 666 or 894, which is necessary to my project, i got a KeyError exception that crashes application.

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

Replace each part with your own scenario:

  1. Create a file with:
from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/")
def read_root():
    return Response(status_code=894)

Expected behavior

Can assign any integer of three characters

Environment

  • OS: [e.g. Linux /macOS]
  • FastAPI Version [e.g. 0.54.1], get it with:
python -c "import fastapi; print(fastapi.__version__)"
  • Python version, get it with:
python --version

3.8.2

Additional context

Traceback (most recent call last): File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi result = await app(self.scope, self.receive, self.send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/fastapi/applications.py", line 149, in __call__ await super().__call__(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/applications.py", line 102, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__ raise exc from None File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__ await self.app(scope, receive, _send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__ raise exc from None File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__ await self.app(scope, receive, sender) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__ await route.handle(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle await self.app(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/routing.py", line 44, in app await response(scope, receive, send) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/responses.py", line 125, in __call__ "headers": self.raw_headers, File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/exceptions.py", line 68, in sender await send(message) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/starlette/middleware/errors.py", line 156, in _send await send(message) File "/Users/ab/.virtualenvs/flask_mock/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 458, in send content = [STATUS_LINE[status_code]] KeyError: 666

bug

All 4 comments

This is intended behaviour; other status codes are in violation of the HTTP spec. And anyway, this is an issue with uvicorn, not FastAPI.

Again if you insist, either ask for help from uvicorn, or probably use another ASGI server:

https://github.com/encode/uvicorn/blob/70f023e78415b45214f8cfe4de8a11acb710e51f/uvicorn/protocols/http/httptools_impl.py#L25-L27

Thank you, it works!

Thanks for the help here everyone! :clap: :bow:

Thanks for reporting back and closing the issue :+1:

Was this page helpful?
0 / 5 - 0 ratings