Skip to content

Commit eca5306

Browse files
committed
Merge pull request pallets#1262 from msabramo/JSONIFY_END_WITH_NEWLINE
Add JSONIFY_END_WITH_NEWLINE config variable
2 parents f166894 + d9402fc commit eca5306

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

CHANGES

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ Version 1.0
5353
- Add "pretty" and "compressed" separators definitions in jsonify() method.
5454
Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing
5555
unnecessary white space included by default after separators.
56-
56+
- JSON responses are now terminated with a newline character, because it is a
57+
convention that UNIX text files end with a newline and some clients don't
58+
deal well when this newline is missing. See
59+
https://github.com/mitsuhiko/flask/pull/1262 -- this came up originally as a
60+
part of https://github.com/kennethreitz/httpbin/issues/168
5761

5862
Version 0.10.2
5963
--------------

flask/json.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ def htmlsafe_dump(obj, fp, **kwargs):
200200

201201
def jsonify(*args, **kwargs):
202202
"""Creates a :class:`~flask.Response` with the JSON representation of
203-
the given arguments with an :mimetype:`application/json` mimetype. The arguments
204-
to this function are the same as to the :class:`dict` constructor.
203+
the given arguments with an :mimetype:`application/json` mimetype. The
204+
arguments to this function are the same as to the :class:`dict`
205+
constructor.
205206
206207
Example usage::
207208
@@ -241,9 +242,13 @@ def get_current_user():
241242
indent = 2
242243
separators = (', ', ': ')
243244

244-
return current_app.response_class(dumps(dict(*args, **kwargs),
245-
indent=indent, separators=separators),
245+
# Note that we add '\n' to end of response
246+
# (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'),
246250
mimetype='application/json')
251+
return rv
247252

248253

249254
def tojson_filter(obj, **kwargs):

tests/test_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def test_make_response_with_response_instance():
942942
rv = flask.make_response(
943943
flask.jsonify({'msg': 'W00t'}), 400)
944944
assert rv.status_code == 400
945-
assert rv.data == b'{\n "msg": "W00t"\n}'
945+
assert rv.data == b'{\n "msg": "W00t"\n}\n'
946946
assert rv.mimetype == 'application/json'
947947

948948
rv = flask.make_response(
@@ -963,7 +963,7 @@ def test_jsonify_no_prettyprint():
963963
app = flask.Flask(__name__)
964964
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
965965
with app.test_request_context():
966-
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}'
966+
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'
967967
uncompressed_msg = {
968968
"msg": {
969969
"submsg": "W00t"
@@ -982,7 +982,7 @@ def test_jsonify_prettyprint():
982982
with app.test_request_context():
983983
compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
984984
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'
986986

987987
rv = flask.make_response(
988988
flask.jsonify(compressed_msg), 200)

0 commit comments

Comments
 (0)