I have readed return-json-response-from-flask-view and flask-response-vs-flask-make-response. And I still have the question:
Can I do json response with make_response
or have to use app.response_class
? Because they differ !!
But in real code make_response
works as well.
Look this code
import json
from flask import Flask, make_response
app = Flask(__name__)
data = {'easy_key': 'any_value'}
with app.app_context():
resp_maked = make_response(json.dumps(data))
resp_maked.headers['Content-Type'] = 'application/json'
resp_classed = app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
print(resp_maked == resp_classed)
It gives False
. Btw
In [5]: resp_maked.headers
Out[5]: Headers([('Content-Type', 'application/json'), ('Content-Length', '25')])
In [6]: resp_classed.headers
Out[6]: Headers([('Content-Type', 'application/json'), ('Content-Length', '25')])
In [7]: resp_maked.json == resp_classed.json
Out[7]: True
In [8]: resp_maked.json
Out[8]: {'easy_key': 'any_value'}
Another question is - what is better to use if I want to reproduce jsonify
by myself?