diff options
author | Brett <brett@nextglass.co> | 2014-05-24 22:31:28 -0400 |
---|---|---|
committer | Brett <brett@nextglass.co> | 2014-05-24 22:31:28 -0400 |
commit | 7f09a53474a168541c488e6c946c4b767fc2bb8d (patch) | |
tree | 9726f162b613f59195a710a5c5f10414f2d9735c | |
parent | ac2170b9b49b4e27b2e2fb6c05880b7a453c60cb (diff) | |
download | bottle-7f09a53474a168541c488e6c946c4b767fc2bb8d.tar.gz |
don't fail when request content type is application/json but the body is empty
-rw-r--r-- | bottle.py | 5 | ||||
-rwxr-xr-x | test/test_environ.py | 8 |
2 files changed, 12 insertions, 1 deletions
@@ -1117,7 +1117,10 @@ class BaseRequest(object): exhaustion. ''' ctype = self.environ.get('CONTENT_TYPE', '').lower().split(';')[0] if ctype == 'application/json': - return json_loads(self._get_body_string()) + b = self._get_body_string() + if not b: + return None + return json_loads(b) return None def _iter_body(self, read, bufsize): diff --git a/test/test_environ.py b/test/test_environ.py index b693e83..2b8079b 100755 --- a/test/test_environ.py +++ b/test/test_environ.py @@ -394,6 +394,14 @@ class TestRequest(unittest.TestCase): e['CONTENT_LENGTH'] = str(len(json_dumps(test))) self.assertEqual(BaseRequest(e).json, None) + def test_json_header_empty_body(self): + """Request Content-Type is application/json but body is empty""" + e = {'CONTENT_TYPE': 'application/json'} + wsgiref.util.setup_testing_defaults(e) + wsgiref.util.setup_testing_defaults(e) + e['CONTENT_LENGTH'] = "0" + self.assertEqual(BaseRequest(e).json, None) + def test_isajax(self): e = {} wsgiref.util.setup_testing_defaults(e) |