Skip to content

Handle JSON body starting with double quote #355

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aikido_zen/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def set_body_internal(self, body):
self.body = self.body.decode("utf-8") # Decode byte input to string.
if not isinstance(self.body, str):
return
if self.body.strip()[0] in ["{", "["]:
if self.body.strip()[0] in ["{", "[", '"']:
# Might be JSON, but might not have been parsed correctly by server because of wrong headers
parsed_body = json.loads(self.body)
if parsed_body:
Expand Down
14 changes: 14 additions & 0 deletions aikido_zen/context/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ def test_set_valid_json_with_spaces():
assert context.body == {"key": [1, 2, 3]}


def test_valid_json_string_with_newlines():
context = Context(req=basic_wsgi_req, body=None, source="flask")

context.set_body('\r\n\r\n"hello"\r\n\r\n')
assert context.body == "hello"


def test_valid_json_string_with_spaces():
context = Context(req=basic_wsgi_req, body=None, source="flask")

context.set_body('" hello "')
assert context.body == " hello "


def test_set_valid_json_with_newlines():
context = Context(req=basic_wsgi_req, body=None, source="flask")

Expand Down