Make `jsonify` terminate responses with a newline
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This came up in the context of https://github.com/kennethreitz/httpbin/issues/168
authored by Marc Abramowitz · committed by Marc Abramowitz · parent b24438b · Browse files · 3 files·+17 added−8 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+17 added−8 removed
- tests:+3 added−3 removed
- other:+14 added−5 removed
Provenance
- Commit
d9402fc- Tree
809b54c- Author
Marc Abramowitz <marc@marc-abramowitz.com>- Committer
Marc Abramowitz <marc@marc-abramowitz.com>- Parent
b24438b
3 files changed+17 added−8 removed
modified:flask/json.py+9 added-4 removed
198 unmodified lines199 200 201 def jsonify(*args, **kwargs):202 """Creates a :class:`~flask.Response` with the JSON representation of203- the given arguments with an :mimetype:`application/json` mimetype. The arguments204- to this function are the same as to the :class:`dict` constructor.203+ the given arguments with an :mimetype:`application/json` mimetype. The204+ arguments to this function are the same as to the :class:`dict`205+ constructor.206 207 Example usage::208 209 from flask import jsonify31 unmodified lines241 and not request.is_xhr:242 indent = 2243 separators = (', ', ': ')244 244- return current_app.response_class(dumps(dict(*args, **kwargs),245- indent=indent, separators=separators),245+ # Note that we add '\n' to end of response246+ # (see https://github.com/mitsuhiko/flask/pull/1262)247+ rv = current_app.response_class(248+ (dumps(dict(*args, **kwargs), indent=indent, separators=separators),249+ '\n'),250 mimetype='application/json')251+ return rv252 253 254 def tojson_filter(obj, **kwargs):255 return Markup(htmlsafe_dumps(obj, **kwargs))modified:tests/test_basic.py+3 added-3 removed
940 unmodified lines941 with app.test_request_context():942 rv = flask.make_response(943 flask.jsonify({'msg': 'W00t'}), 400)944 assert rv.status_code == 400945- assert rv.data == b'{\n "msg": "W00t"\n}'945+ assert rv.data == b'{\n "msg": "W00t"\n}\n'946 assert rv.mimetype == 'application/json'947 948 rv = flask.make_response(949 flask.Response(''), 400)12 unmodified lines962 def test_jsonify_no_prettyprint():963 app = flask.Flask(__name__)964 app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})965 with app.test_request_context():966- compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}'966+ compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'967 uncompressed_msg = {968 "msg": {969 "submsg": "W00t"970 },10 unmodified lines981 app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True})982 with app.test_request_context():983 compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}984 pretty_response =\985- b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}'985+ b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'986 987 rv = flask.make_response(988 flask.jsonify(compressed_msg), 200)989 assert rv.data == pretty_response577 unmodified linesmodified:CHANGES+5 added-1 removed
51 unmodified lines52 - Deprecated ``request.json`` in favour of ``request.get_json()``.53 - Add "pretty" and "compressed" separators definitions in jsonify() method.54 Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing55 unnecessary white space included by default after separators.56+- JSON responses are now terminated with a newline character, because it is a57+ convention that UNIX text files end with a newline and some clients don't58+ deal well when this newline is missing. See59+ https://github.com/mitsuhiko/flask/pull/1262 -- this came up originally as a60+ part of https://github.com/kennethreitz/httpbin/issues/16861 57-62 Version 0.10.263 --------------64 65 (bugfix release, release date to be announced)477 unmodified lines