Skip to content

Commit 0ace04d

Browse files
revimiuntitaker
authored andcommitted
feat: aiohttp request body (#527)
* feat: aiohttp request body * feat: placeholder text for the case when aiohttp request body can't be read * tests: add tests for getting aiohttp request body * tests: fixes for tests
1 parent 39120dc commit 0ace04d

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from aiohttp.abc import AbstractMatchInfo
2525
from typing import Any
2626
from typing import Dict
27+
from typing import Optional
2728
from typing import Tuple
2829
from typing import Callable
2930

@@ -136,6 +137,7 @@ def aiohttp_processor(
136137
_filter_headers(dict(request.headers)),
137138
should_repr_strings=False,
138139
)
140+
request_info["data"] = get_aiohttp_request_data(request)
139141

140142
return event
141143

@@ -152,3 +154,23 @@ def _capture_exception(hub):
152154
)
153155
hub.capture_event(event, hint=hint)
154156
return exc_info
157+
158+
159+
BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"
160+
161+
162+
def get_aiohttp_request_data(request):
163+
# type: (Request) -> Optional[str]
164+
bytes_body = request._read_bytes
165+
166+
if bytes_body is not None:
167+
# we have body to show
168+
encoding = request.charset or "utf-8"
169+
return bytes_body.decode(encoding)
170+
171+
if request.can_read_body:
172+
# body exists but we can't show it
173+
return BODY_NOT_READ_MESSAGE
174+
175+
# request has no body
176+
return None

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from aiohttp import web
24

35
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
@@ -33,6 +35,7 @@ async def hello(request):
3335
assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
3436
assert request["method"] == "GET"
3537
assert request["query_string"] == ""
38+
assert request.get("data") is None
3639
assert request["url"] == "http://{host}/".format(host=host)
3740
assert request["headers"] == {
3841
"Accept": "*/*",
@@ -42,6 +45,63 @@ async def hello(request):
4245
}
4346

4447

48+
async def test_post_body_not_read(sentry_init, aiohttp_client, loop, capture_events):
49+
from sentry_sdk.integrations.aiohttp import BODY_NOT_READ_MESSAGE
50+
51+
sentry_init(integrations=[AioHttpIntegration()])
52+
53+
body = {"some": "value"}
54+
55+
async def hello(request):
56+
1 / 0
57+
58+
app = web.Application()
59+
app.router.add_post("/", hello)
60+
61+
events = capture_events()
62+
63+
client = await aiohttp_client(app)
64+
resp = await client.post("/", json=body)
65+
assert resp.status == 500
66+
67+
event, = events
68+
exception, = event["exception"]["values"]
69+
assert exception["type"] == "ZeroDivisionError"
70+
request = event["request"]
71+
72+
assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
73+
assert request["method"] == "POST"
74+
assert request["data"] == BODY_NOT_READ_MESSAGE
75+
76+
77+
async def test_post_body_read(sentry_init, aiohttp_client, loop, capture_events):
78+
sentry_init(integrations=[AioHttpIntegration()])
79+
80+
body = {"some": "value"}
81+
82+
async def hello(request):
83+
await request.json()
84+
1 / 0
85+
86+
app = web.Application()
87+
app.router.add_post("/", hello)
88+
89+
events = capture_events()
90+
91+
client = await aiohttp_client(app)
92+
resp = await client.post("/", json=body)
93+
assert resp.status == 500
94+
95+
event, = events
96+
exception, = event["exception"]["values"]
97+
assert exception["type"] == "ZeroDivisionError"
98+
request = event["request"]
99+
100+
assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
101+
assert request["method"] == "POST"
102+
assert request["data"] == json.dumps(body)
103+
104+
45105
async def test_403_not_captured(sentry_init, aiohttp_client, loop, capture_events):
46106
sentry_init(integrations=[AioHttpIntegration()])
47107

0 commit comments

Comments
 (0)