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.
Steps to reproduce the behavior with a minimum self-contained file.
Replace each part with your own scenario:
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/")
def read_root():
return Response(status_code=894)
Can assign any integer of three characters
python -c "import fastapi; print(fastapi.__version__)"
python --version
3.8.2
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
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:
Thank you, it works!
Thanks for the help here everyone! :clap: :bow:
Thanks for reporting back and closing the issue :+1: