Skip to content

Commit

Permalink
Fix post bad json, #78
Browse files Browse the repository at this point in the history
  • Loading branch information
meyt committed Jul 7, 2018
1 parent 8dbbf66 commit 0e86b72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nanohttp/helpers.py
Expand Up @@ -123,7 +123,10 @@ def parse_any_form(environ, content_length=None, content_type=None):
if content_length and content_type == 'application/json':
fp = environ['wsgi.input']
data = fp.read(content_length)
return ujson.decode(data)
try:
return ujson.decode(data)
except ValueError:
raise exceptions.HttpBadRequest('Cannot parse the request')

try:
storage = cgi.FieldStorage(
Expand Down
5 changes: 5 additions & 0 deletions nanohttp/tests/test_forms.py
Expand Up @@ -57,6 +57,11 @@ def test_json(self):
}),
expected_response='a: 1, b: [2, 3]',
)
self.assert_post(
'/',
json='{',
status=400
)

def test_invalid_form(self):
self.assert_post(
Expand Down

0 comments on commit 0e86b72

Please sign in to comment.