Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h11._util.LocalProtocolError: Too little data for declared Content-Length when open FastAPI docs #10

Closed
chrismaille opened this issue Feb 14, 2022 · 3 comments · Fixed by #27

Comments

@chrismaille
Copy link

Dependencies:

  • fastapi 0.73.0
  • starlette 0.17.1
  • pydantic 1.9.0
  • h11 0.12.0

Traceback

When trying to open http://localhost:XXXX/docs:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 364, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 212, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 64, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 244, in __call__
    await wrap(partial(self.listen_for_disconnect, receive))
  File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 574, in __aexit__
    raise exceptions[0]
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 240, in wrap
    await func()
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 234, in stream_response
    await send({"type": "http.response.body", "body": b"", "more_body": False})
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 156, in _send
    await send(message)
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 468, in send
    output = self.conn.send(event)
  File "/usr/local/lib/python3.9/site-packages/h11/_connection.py", line 468, in send
    data_list = self.send_with_data_passthrough(event)
  File "/usr/local/lib/python3.9/site-packages/h11/_connection.py", line 501, in send_with_data_passthrough
    writer(event, data_list.append)
  File "/usr/local/lib/python3.9/site-packages/h11/_writers.py", line 60, in __call__
    self.send_eom(event.headers, write)
  File "/usr/local/lib/python3.9/site-packages/h11/_writers.py", line 83, in send_eom
    raise LocalProtocolError("Too little data for declared Content-Length")
h11._util.LocalProtocolError: Too little data for declared Content-Length

The issue (I think)

async for body in response.body_iterator: # type: ignore

On first iteration, body equals the correct body from response (b'<HTML CONTENT>'), on second iteration body is empty (b''). This empty body is use on the subsequent code, inserted on response, but line 97 never occurs.

chrismaille added a commit to chrismaille/fastapi-debug-toolbar that referenced this issue Feb 19, 2022
* Fix h11._util.LocalProtocolError: Too little data for declared Content-Length when open FastAPI docs/swagger
* Fix missing table error in unit tests, when sqlite drops table on memory after closing session.
* Fix Generator-type Session dependencies not asserting AsyncExitStack. Use the settings `session_generators` option to pass the full python path for your generator: "foo.bar:my_db_session"
* Do not show database credential on panels
* Fix json encoding error using special UUID and datetime types in python
* Show loguru logs in logging panel

Resolves mongkok#10
@andreipradan
Copy link

@chrismaille
Was this issue fixed in a different way?
I've seen the related PR was closed and not merged

@YaoJusheng
Copy link

@mongkok

The same error occured in my code. Was this solution worked ?

@mongkok
Copy link
Owner

mongkok commented Dec 16, 2022

The issue was fixed, please try the latest release 0.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants