Browse Source

Added support for custom JSON mimetypes

pull/957/merge
Armin Ronacher 11 years ago
parent
commit
e5bba9deb5
  1. 1
      CHANGES
  2. 9
      flask/testsuite/helpers.py
  3. 17
      flask/wrappers.py

1
CHANGES

@ -14,6 +14,7 @@ Version 1.0
`False` it will only be modified if the session actually modifies. `False` it will only be modified if the session actually modifies.
Non permanent sessions are not affected by this and will always Non permanent sessions are not affected by this and will always
expire if the browser window closes. expire if the browser window closes.
- Made Flask support custom JSON mimetypes for incoming data.
Version 0.10.2 Version 0.10.2
-------------- --------------

9
flask/testsuite/helpers.py

@ -38,6 +38,15 @@ class JSONTestCase(FlaskTestCase):
rv = c.post('/json', data='malformed', content_type='application/json') rv = c.post('/json', data='malformed', content_type='application/json')
self.assert_equal(rv.status_code, 400) self.assert_equal(rv.status_code, 400)
def test_json_custom_mimetypes(self):
app = flask.Flask(__name__)
@app.route('/json', methods=['POST'])
def return_json():
return flask.request.get_json()
c = app.test_client()
rv = c.post('/json', data='"foo"', content_type='application/x+json')
self.assert_equal(rv.data, b'foo')
def test_json_body_encoding(self): def test_json_body_encoding(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.testing = True app.testing = True

17
flask/wrappers.py

@ -107,6 +107,21 @@ class Request(RequestBase):
# XXX: deprecate property # XXX: deprecate property
return self.get_json() return self.get_json()
@property
def is_json(self):
"""Indicates if this request is JSON or not. By default a request
is considered to include JSON data if the mimetype is
``application/json`` or ``application/*+json``.
.. versionadded:: 0.11
"""
mt = self.mimetype
if mt == 'application/json':
return True
if mt.startswith('application/') and mt.endswith('+json'):
return True
return False
def get_json(self, force=False, silent=False, cache=True): def get_json(self, force=False, silent=False, cache=True):
"""Parses the incoming JSON request data and returns it. If """Parses the incoming JSON request data and returns it. If
parsing fails the :meth:`on_json_loading_failed` method on the parsing fails the :meth:`on_json_loading_failed` method on the
@ -124,7 +139,7 @@ class Request(RequestBase):
if rv is not _missing: if rv is not _missing:
return rv return rv
if self.mimetype != 'application/json' and not force: if not (force or self.is_json):
return None return None
# We accept a request charset against the specification as # We accept a request charset against the specification as

Loading…
Cancel
Save