mirror of https://github.com/pallets/flask.git
Added support for custom JSON mimetypes
This commit is contained in:
parent
f5a02b9495
commit
e5bba9deb5
1
CHANGES
1
CHANGES
|
@ -14,6 +14,7 @@ Version 1.0
|
|||
`False` it will only be modified if the session actually modifies.
|
||||
Non permanent sessions are not affected by this and will always
|
||||
expire if the browser window closes.
|
||||
- Made Flask support custom JSON mimetypes for incoming data.
|
||||
|
||||
Version 0.10.2
|
||||
--------------
|
||||
|
|
|
@ -38,6 +38,15 @@ class JSONTestCase(FlaskTestCase):
|
|||
rv = c.post('/json', data='malformed', content_type='application/json')
|
||||
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):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
|
|
@ -107,6 +107,21 @@ class Request(RequestBase):
|
|||
# XXX: deprecate property
|
||||
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):
|
||||
"""Parses the incoming JSON request data and returns it. If
|
||||
parsing fails the :meth:`on_json_loading_failed` method on the
|
||||
|
@ -124,7 +139,7 @@ class Request(RequestBase):
|
|||
if rv is not _missing:
|
||||
return rv
|
||||
|
||||
if self.mimetype != 'application/json' and not force:
|
||||
if not (force or self.is_json):
|
||||
return None
|
||||
|
||||
# We accept a request charset against the specification as
|
||||
|
|
Loading…
Reference in New Issue