I can't figure out how to force the MIME type application/json
for a view in Flask. Here is a simple view I've thrown together for demonstration purposes:
@app.route("/")
def testView():
ret = '{"data": "JSON string example"}'
return ret
The JSON string (held in variable ret
) is gathered from elsewhere (using stdout from another program using subprocess
) so I can't use jsonify
provided with Flask.
I've had a look at the "Returning Json" Documentation and this Stackoverflow question but I haven't had any luck so far. I've been looking around for awhile now & will continue searching but thought I'd ask here just in case anyone has come across this.
Thanks.
See the answer below
The solution:
@app.route("/")
def testView():
ret = '{"data": "JSON string example"}'
resp = Response(response=ret,
status=200,
mimetype="application/json")
return resp
I found this website useful: Implementing a RESTful Web API with Python & Flask
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…