summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett <brett@nextglass.co>2014-05-24 22:31:28 -0400
committerBrett <brett@nextglass.co>2014-05-24 22:31:28 -0400
commit7f09a53474a168541c488e6c946c4b767fc2bb8d (patch)
tree9726f162b613f59195a710a5c5f10414f2d9735c
parentac2170b9b49b4e27b2e2fb6c05880b7a453c60cb (diff)
downloadbottle-7f09a53474a168541c488e6c946c4b767fc2bb8d.tar.gz
don't fail when request content type is application/json but the body is empty
-rw-r--r--bottle.py5
-rwxr-xr-xtest/test_environ.py8
2 files changed, 12 insertions, 1 deletions
diff --git a/bottle.py b/bottle.py
index bcc284c..583c213 100644
--- a/bottle.py
+++ b/bottle.py
@@ -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)