Add JSONIFY_MIMETYPE configuration variable (#1728)

Allow jsonify responses' mimetype to be configured
This commit is contained in:
Steven Loria 2016-04-08 19:30:47 -03:00 committed by David Lord
parent 0690ce18c2
commit 2bf477cfea
5 changed files with 16 additions and 1 deletions

View File

@ -76,6 +76,7 @@ Version 1.0
- ``flask.ext`` is now deprecated (pull request ``#1484``). - ``flask.ext`` is now deprecated (pull request ``#1484``).
- ``send_from_directory`` now raises BadRequest if the filename is invalid on - ``send_from_directory`` now raises BadRequest if the filename is invalid on
the server OS (pull request ``#1763``). the server OS (pull request ``#1763``).
- Added the ``JSONIFY_MIMETYPE`` configuration variable (pull request ``#1728``).
Version 0.10.2 Version 0.10.2
-------------- --------------

View File

@ -183,6 +183,7 @@ The following configuration values are used internally by Flask:
if they are not requested by an if they are not requested by an
XMLHttpRequest object (controlled by XMLHttpRequest object (controlled by
the ``X-Requested-With`` header) the ``X-Requested-With`` header)
``JSONIFY_MIMETYPE`` MIME type used for jsonify responses.
``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of ``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of
the template source and reload it the template source and reload it
automatically. By default the value is automatically. By default the value is

View File

@ -315,6 +315,7 @@ class Flask(_PackageBoundObject):
'JSON_AS_ASCII': True, 'JSON_AS_ASCII': True,
'JSON_SORT_KEYS': True, 'JSON_SORT_KEYS': True,
'JSONIFY_PRETTYPRINT_REGULAR': True, 'JSONIFY_PRETTYPRINT_REGULAR': True,
'JSONIFY_MIMETYPE': 'application/json',
'TEMPLATES_AUTO_RELOAD': None, 'TEMPLATES_AUTO_RELOAD': None,
}) })

View File

@ -264,7 +264,7 @@ def jsonify(*args, **kwargs):
return current_app.response_class( return current_app.response_class(
(dumps(data, indent=indent, separators=separators), '\n'), (dumps(data, indent=indent, separators=separators), '\n'),
mimetype='application/json' mimetype=current_app.config['JSONIFY_MIMETYPE']
) )

View File

@ -1019,6 +1019,18 @@ def test_jsonify_prettyprint():
assert rv.data == pretty_response assert rv.data == pretty_response
def test_jsonify_mimetype():
app = flask.Flask(__name__)
app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'})
with app.test_request_context():
msg = {
"msg": {"submsg": "W00t"},
}
rv = flask.make_response(
flask.jsonify(msg), 200)
assert rv.mimetype == 'application/vnd.api+json'
def test_url_generation(): def test_url_generation():
app = flask.Flask(__name__) app = flask.Flask(__name__)