uvicorn's "Response content longer than Content-Length" RuntimeError for DELETE endpoints #1635
-
When dealing with DELETE endpoints that have an empty JSONResponse content and 204 status, e.g.: from starlette.applications import Starlette
from starlette.responses import JSONResponse, PlainTextResponse
from starlette.routing import Route
async def homepage(request):
return JSONResponse(None, status_code=204)
# return PlainTextResponse(None, status_code=204)
routes = [
Route("/", endpoint=homepage, methods=["DELETE"])
]
starapp = Starlette(routes=routes) and run it with uvicorn, like this:
we get this error from uvicorn:
This happens since starlette Note that this doesn't happen with I'm also not sure how to debug uvicorn server per se, {'body': b'null',
'message': {'body': b'null', 'type': 'http.response.body'},
'message_type': 'http.response.body',
'more_body': False,
'num_bytes': 4,
'self': <uvicorn.protocols.http.httptools_impl.RequestResponseCycle object at 0x1105809a0>} If instead of
with the same error from uvicorn. Also, note that I originally noticed that when using FastAPI v0.76. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Hey slafs I tried to reproduce it and a similar error raised:
Also I've found that is not related to DELETE verb and when you replace it with different verbs like GET, POST, ... nothing changes. Then, I tried to reproduce it with other ASGI workers like daphne and hypercorn and I noticed that everything works well. So, It's probably an issue with Thanks for reporting. |
Beta Was this translation helpful? Give feedback.
-
This is not a uvicorn issue. Uvicorn does what is right here. Starlette could document better the |
Beta Was this translation helpful? Give feedback.
-
I think It's better to put the problem in an issue. |
Beta Was this translation helpful? Give feedback.
-
Another, maybe simpler example, that reproduces the problem: from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import Response
from starlette.requests import Request
def endpoint(request: Request):
return Response("null", status_code=204)
app = Starlette(
routes=[Route("/", endpoint=endpoint)],
) The problem is even more notorious when using FastAPI, as the same response from above is generated automatically because a function implicitly always returns with from fastapi import FastAPI
app = FastAPI()
@app.get("/", status_code=204)
def endpoint():
print("Oh, hi mark") I created an issue here with a lot more details: #1764, I'm gonna work on it. |
Beta Was this translation helpful? Give feedback.
This is not a uvicorn issue. Uvicorn does what is right here.
Starlette could document better the
JSONResponse
interaction with status codes that should not have any content. In this case what is happening is that Starlette'sJSONResponse
is sending 4 bytes (null), but there's noContent-Length
header, and the status code is 204.