Allow dictionary return values as JSON

This supports an increasingly common usecase whereby JSON is the
primary response (rather than a templated string). Given Flask has a
short syntax for HTML reponses, it seems fitting that it should also
do so for JSON responses. In practice it allows,

     @app.route("/")
     def index():
         return {
             "api_stuff": "values",
         }
This commit is contained in:
pgjones 2019-03-03 17:34:29 +00:00 committed by David Lord
parent 2616d97f32
commit 7bf8366970
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
4 changed files with 37 additions and 16 deletions

View file

@ -1147,8 +1147,12 @@ def test_response_types(app, client):
def from_wsgi():
return NotFound()
assert client.get("/text").data == u"Hällo Wörld".encode("utf-8")
assert client.get("/bytes").data == u"Hällo Wörld".encode("utf-8")
@app.route('/dict')
def from_dict():
return {"foo": "bar"}, 201
assert client.get('/text').data == u'Hällo Wörld'.encode('utf-8')
assert client.get('/bytes').data == u'Hällo Wörld'.encode('utf-8')
rv = client.get("/full_tuple")
assert rv.data == b"Meh"
@ -1181,6 +1185,10 @@ def test_response_types(app, client):
assert b"Not Found" in rv.data
assert rv.status_code == 404
rv = client.get('/dict')
assert rv.json == {"foo": "bar"}
assert rv.status_code == 201
def test_response_type_errors():
app = flask.Flask(__name__)