PacktPublishing / Building-Data-Science-Applications-with-FastAPI

Building Data Science Applications with FastAPI, Published by Packt
MIT License
306 stars 157 forks source link

Too much data for declared Content-Length error in DELETE post (Chapter 3) #4

Closed particle1331 closed 2 years ago

particle1331 commented 2 years ago

I'm getting the following error message when testing out chapter3_response_path_parameters_02.py.

h11._util.LocalProtocolError: Too much data for declared Content-Length

This looks like a known issue. I'm guessing its because content-length: 4 in the response when returning None. Returning Response(status_code=status.HTTP_204_NO_CONTENT) seems to fix the issue (content-length zero in this case).

Screen Shot 2021-12-25 at 5 02 48 AM
frankie567 commented 2 years ago

Hi @particle1331!

Yes, it's a (yet) unresolved issue in FastAPI. Here is the main issue tracking this: https://github.com/tiangolo/fastapi/issues/2832

The shortest solution is the following:

@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT, response_class=Response)
async def delete_post(id: int):
    posts.pop(id, None)
    return None

I made the choice to not mention it in the book because it'll (hopefully) be fixed at some point.

Note also that it happens when Uvicorn run the h11 HTTP implementation. When using the httptools implementation, the error is ignored. If you installed Uvicorn with uvicorn[standard], httptools should be used by default. You can try to force the implementation using the --http flag:

uvicorn --http httptools chapter3_response_path_parameters_02:app