title: Forcing application/json MIME type in a view (Flask)
body: <p>I can't figure out how to force the MIME type <code>application/json</code> for a view in Flask. Here is a simple view I've thrown together for demonstration purposes:</p>
  
  <pre><code>@app.route("/")
  def testView():
      ret = '{"data": "JSON string example"}'
      return ret
  </code></pre>
  
  <p>The JSON string (held in variable <code>ret</code>) is gathered from elsewhere (using stdout from another program using <a href="http://docs.python.org/library/subprocess.html" rel="noreferrer"><code>subprocess</code></a>) so I can't use <a href="http://flask.pocoo.org/docs/api/#flask.jsonify" rel="noreferrer"><code>jsonify</code></a> provided with Flask.</p>
  
  <p>I've had a look at the <a href="http://flask.pocoo.org/docs/api/#returning-json" rel="noreferrer">"Returning Json" Documentation</a> and <a href="https://stackoverflow.com/questions/7086448/">this</a> Stackoverflow question but I haven't had any luck so far. I've been looking around for awhile now &amp; will continue searching but thought I'd ask here just in case anyone has come across this.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>See the <a href="https://stackoverflow.com/a/11945643/586621">answer below</a></p>
  
  <p>The solution:</p>
  
  <pre><code>@app.route("/")
  def testView():
      ret = '{"data": "JSON string example"}'
  
      resp = Response(response=ret,
                      status=200,
                      mimetype="application/json")
  
      return resp
  </code></pre>
  
  <p>I found this website useful: <a href="http://blog.luisrei.com/articles/flaskrest.html" rel="noreferrer">Implementing a RESTful Web API with Python &amp; Flask</a></p>