Added support for custom JSON mimetypes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent f5a02b9 · Browse files · 3 files·+26 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+26 added−1 removed
- other:+26 added−1 removed
Provenance
- Commit
e5bba9d- Tree
1415277- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f5a02b9
3 files changed+26 added−1 removed
modified:flask/testsuite/helpers.py+9 added
36 unmodified lines37 c = app.test_client()38 rv = c.post('/json', data='malformed', content_type='application/json')39 self.assert_equal(rv.status_code, 400)40 41+ def test_json_custom_mimetypes(self):42+ app = flask.Flask(__name__)43+ @app.route('/json', methods=['POST'])44+ def return_json():45+ return flask.request.get_json()46+ c = app.test_client()47+ rv = c.post('/json', data='"foo"', content_type='application/x+json')48+ self.assert_equal(rv.data, b'foo')49+50 def test_json_body_encoding(self):51 app = flask.Flask(__name__)52 app.testing = True53 @app.route('/')579 unmodified linesmodified:flask/wrappers.py+16 added-1 removed
105 unmodified lines106 """107 # XXX: deprecate property108 return self.get_json()109 110+ @property111+ def is_json(self):112+ """Indicates if this request is JSON or not. By default a request113+ is considered to include JSON data if the mimetype is114+ ``application/json`` or ``application/*+json``.115+116+ .. versionadded:: 0.11117+ """118+ mt = self.mimetype119+ if mt == 'application/json':120+ return True121+ if mt.startswith('application/') and mt.endswith('+json'):122+ return True123+ return False124+125 def get_json(self, force=False, silent=False, cache=True):126 """Parses the incoming JSON request data and returns it. If127 parsing fails the :meth:`on_json_loading_failed` method on the128 request object will be invoked. By default this function will9 unmodified lines138 rv = getattr(self, '_cached_json', _missing)139 if rv is not _missing:140 return rv141 127- if self.mimetype != 'application/json' and not force:142+ if not (force or self.is_json):143 return None144 145 # We accept a request charset against the specification as146 # certain clients have been using this in the past. This53 unmodified linesmodified:CHANGES+1 added
12 unmodified lines13 refreshed each request and get their lifetime extended, if set to14 `False` it will only be modified if the session actually modifies.15 Non permanent sessions are not affected by this and will always16 expire if the browser window closes.17+- Made Flask support custom JSON mimetypes for incoming data.18 19 Version 0.10.220 --------------21 469 unmodified lines