Skip to content

Commit

Permalink
json_body: leave the bodyfile in the position where it was before we …
Browse files Browse the repository at this point in the history
…started reading.
  • Loading branch information
mauritsvanrees committed Nov 2, 2023
1 parent 0e6f407 commit a1d75b4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/plone/restapi/deserializer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ def json_body(request):
if bodyfile is None:
data = {}
else:
if bodyfile.tell() != 0:
# Something has already read the bodyfile.
try:
fpos = bodyfile.tell()
except Exception:
fpos = None
if fpos:
# Something has already begun reading the bodyfile.
# Go back to the beginning.
bodyfile.seek(0)
try:
data = json.load(bodyfile)
except ValueError:
raise DeserializationError("No JSON object could be decoded")
finally:
if fpos is not None:
# Go back to the previous position.
bodyfile.seek(fpos)
if not isinstance(data, dict):
raise DeserializationError("Malformed body")
return data
Expand Down

0 comments on commit a1d75b4

Please sign in to comment.