Skip to content

Commit 439657e

Browse files
authored
Merge pull request #117 from sapcc/handle-204-No-Content
Fix: Prevent JSONDecodeError for empty request body
2 parents 65e57c7 + b42cb21 commit 439657e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

auditmiddleware/_api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,13 @@ def _create_events(self, target_project, res_id,
416416
# which contains a list of items
417417
res_pl = res_payload[res_spec.type_name]
418418
req_pl = None
419+
# Ensure request has a JSON body before accessing request.json
419420
if self._payloads_enabled and res_spec.payloads['enabled'] \
420-
and request.content_type == 'application/json':
421-
req_pl = iter(request.json.get(res_spec.type_name))
421+
and request.content_type == 'application/json' \
422+
and request.content_length is not None \
423+
and request.content_length > 0:
424+
req_pl_data = request.json
425+
req_pl = iter(req_pl_data.get(res_spec.type_name))
422426

423427
# create one event per item
424428
for subpayload in res_pl:
@@ -474,7 +478,9 @@ def _create_events(self, target_project, res_id,
474478
if event and request.method[0] == 'P' \
475479
and self._payloads_enabled \
476480
and res_spec.payloads['enabled'] \
477-
and request.content_type == 'application/json':
481+
and request.content_type == 'application/json' \
482+
and request.content_length is not None \
483+
and request.content_length > 0:
478484
self._attach_payload(event, request.json, res_spec)
479485

480486
events = [event]

0 commit comments

Comments
 (0)