diff options
| -rw-r--r-- | CHANGES.txt | 5 | ||||
| -rw-r--r-- | docs/api/request.rst | 7 | ||||
| -rw-r--r-- | docs/whatsnew-1.1.rst | 5 | ||||
| -rw-r--r-- | pyramid/request.py | 7 | ||||
| -rw-r--r-- | pyramid/tests/test_request.py | 20 |
5 files changed, 44 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 67163d3e7..0eb02baad 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,11 @@ Next release Features -------- +- New request attribute: ``json``. If the request's ``content_type`` is + ``application/json``, this attribute will contain the JSON-decoded + variant of the request body. If the request's ``content_type`` is not + ``application/json``, this attribute will be ``None``. + - A new value ``http_cache`` can be used as a view configuration parameter. diff --git a/docs/api/request.rst b/docs/api/request.rst index 27ce395ac..5dfb2ae9a 100644 --- a/docs/api/request.rst +++ b/docs/api/request.rst @@ -180,6 +180,13 @@ object (exposed to view code as ``request.response``) to influence rendered response behavior. + .. attribute:: json + + If the request's ``content_type`` is ``application/json``, this + attribute will contain the JSON-decoded variant of the request body. + If the request's ``content_type`` is not ``application/json``, this + attribute will be ``None``. + .. note:: For information about the API of a :term:`multidict` structure (such as diff --git a/docs/whatsnew-1.1.rst b/docs/whatsnew-1.1.rst index 783f2caaa..fdf3b1c74 100644 --- a/docs/whatsnew-1.1.rst +++ b/docs/whatsnew-1.1.rst @@ -94,6 +94,11 @@ Default HTTP Exception View Minor Feature Additions ----------------------- +- New request attribute: ``json``. If the request's ``content_type`` is + ``application/json``, this attribute will contain the JSON-decoded + variant of the request body. If the request's ``content_type`` is not + ``application/json``, this attribute will be ``None``. + - A new value ``http_cache`` can be used as a :term:`view configuration` parameter. diff --git a/pyramid/request.py b/pyramid/request.py index cc5137869..a3848461f 100644 --- a/pyramid/request.py +++ b/pyramid/request.py @@ -10,6 +10,7 @@ from pyramid.interfaces import IResponse from pyramid.interfaces import ISessionFactory from pyramid.interfaces import IResponseFactory +from pyramid.compat import json from pyramid.exceptions import ConfigurationError from pyramid.decorator import reify from pyramid.response import Response @@ -489,6 +490,12 @@ class Request(BaseRequest, DeprecatedRequestMethods): return False return adapted is ob + @property + def json(self): + if self.content_type == 'application/json': + return json.loads(self.body, encoding=self.charset) + + def route_request_iface(name, bases=()): iface = InterfaceClass('%s_IRequest' % name, bases=bases) # for exception view lookups diff --git a/pyramid/tests/test_request.py b/pyramid/tests/test_request.py index 76426b8a8..0c1c78721 100644 --- a/pyramid/tests/test_request.py +++ b/pyramid/tests/test_request.py @@ -233,6 +233,26 @@ class TestRequest(unittest.TestCase): request.registry.registerAdapter(adapter, (Foo,), IResponse) self.assertEqual(request.is_response(foo), True) + def test_json_incorrect_mimetype(self): + request = self._makeOne({}) + self.assertEqual(request.json, None) + + def test_json_correct_mimetype(self): + request = self._makeOne({}) + request.content_type = 'application/json' + request.body = '{"a":1}' + self.assertEqual(request.json, {'a':1}) + + def test_json_alternate_charset(self): + from pyramid.compat import json + request = self._makeOne({}) + request.content_type = 'application/json' + request.charset = 'latin-1' + la = unicode('La Pe\xc3\xb1a', 'utf-8') + body = json.dumps({'a':la}, encoding='latin-1') + request.body = body + self.assertEqual(request.json, {'a':la}) + class TestRequestDeprecatedMethods(unittest.TestCase): def setUp(self): self.config = testing.setUp() |
