Skip to content

Conversation

alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Sep 12, 2025

You can mount any ASGI app to a FastAPI or Starlette route. You can also parse the request body manually using the receive callable on FastAPI's and Starlette's Request objects.

In either case, reading json(), form() or body() in the patched route handler consumes the request body stream, leaving no stream for the application to read. If the user relies on these high-level Starlette methods then there is no issue because Starlette caches the result. Combining our FastAPI or Starlette integrations with low-level ASGI request body parsing is problematic.

Approach in this PR:

  • Attach the request body in the event processor, not in the route handler itself.
  • If the user calls json(), form() or body(), cache the result on the ASGI scope and attach the result to events as before.
  • Cache the raw request body on the ASGI scope. If none of the above methods are called try to parse the request body and attach the result to events. An exception could be raised before any of the above methods are invoked.

Closes #4764

Copy link

codecov bot commented Sep 12, 2025

❌ 11 Tests Failed:

Tests completed Failed Passed Skipped
22013 11 22002 2108
View the top 3 failed test(s) by shortest run time
::tests.integrations.ariadne.test_ariadne
Stack Traces | 0s run time
.../integrations/ariadne/test_ariadne.py:8: in <module>
    from sentry_sdk.integrations.fastapi import FastApiIntegration
sentry_sdk/integrations/fastapi.py:18: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:654: in <module>
    class MultipartPart:
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.ariadne.test_ariadne
Stack Traces | 0s run time
.../integrations/ariadne/test_ariadne.py:8: in <module>
    from sentry_sdk.integrations.fastapi import FastApiIntegration
sentry_sdk/integrations/fastapi.py:18: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:654: in <module>
    class MultipartPart:
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.graphene.test_graphene
Stack Traces | 0s run time
.../integrations/graphene/test_graphene.py:7: in <module>
    from sentry_sdk.integrations.fastapi import FastApiIntegration
sentry_sdk/integrations/fastapi.py:18: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:653: in <module>
    @dataclass
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.starlette.test_starlette
Stack Traces | 0s run time
.../integrations/starlette/test_starlette.py:16: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:654: in <module>
    class MultipartPart:
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.starlette.test_starlette
Stack Traces | 0s run time
.../integrations/starlette/test_starlette.py:16: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:654: in <module>
    class MultipartPart:
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.starlette.test_starlette
Stack Traces | 0s run time
.../integrations/starlette/test_starlette.py:16: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:653: in <module>
    @dataclass
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
::tests.integrations.starlette.test_starlette
Stack Traces | 0s run time
.../integrations/starlette/test_starlette.py:16: in <module>
    from sentry_sdk.integrations.starlette import (
sentry_sdk/integrations/starlette.py:653: in <module>
    @dataclass
sentry_sdk/integrations/starlette.py:655: in MultipartPart
    content_disposition: bytes | None = None
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
tests.integrations.starlette.test_starlette::test_starletterequestextractor_form_bytes
Stack Traces | 0.065s run time
.../integrations/starlette/test_starlette.py:465: in test_starletterequestextractor_form_bytes
    form_data = request_info["data"]
E   TypeError: 'NoneType' object is not subscriptable
tests.integrations.starlette.test_starlette::test_starletterequestextractor_form_bytes
Stack Traces | 0.067s run time
.../integrations/starlette/test_starlette.py:465: in test_starletterequestextractor_form_bytes
    form_data = request_info["data"]
                ^^^^^^^^^^^^^^^^^^^^
E   TypeError: 'NoneType' object is not subscriptable
tests.integrations.starlette.test_starlette::test_starletterequestextractor_form_bytes
Stack Traces | 0.077s run time
.../integrations/starlette/test_starlette.py:465: in test_starletterequestextractor_form_bytes
    form_data = request_info["data"]
                ^^^^^^^^^^^^^^^^^^^^
E   TypeError: 'NoneType' object is not subscriptable
tests.integrations.starlette.test_starlette::test_starletterequestextractor_form_bytes
Stack Traces | 0.082s run time
.../integrations/starlette/test_starlette.py:465: in test_starletterequestextractor_form_bytes
    form_data = request_info["data"]
                ^^^^^^^^^^^^^^^^^^^^
E   TypeError: 'NoneType' object is not subscriptable

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

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

Successfully merging this pull request may close these issues.

Requests to fastapi-mcp hangs if Sentry is enabled
1 participant