- 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``.
| | |
| | | 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. |
| | | |
| | |
| | | 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 |
| | |
| | | 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. |
| | | |
| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | 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() |