title: How do I set response headers in Flask?
body: <p>This is my code:</p>
  <pre><code>@app.route('/hello', methods=[&quot;POST&quot;])
  def hello():
      resp = make_response(render_template('hello.html'))
      resp.headers['Access-Control-Allow-Origin'] = '*'
      return resp
  </code></pre>
  <p>However, when I make a request from the browser to my server I get this error:</p>
  <pre><code>XMLHttpRequest cannot load http://localhost:5000/hello. 
  No 'Access-Control-Allow-Origin' header is present on the requested resource.
  </code></pre>
  <p>I have also tried this approach, setting the response headers &quot;after&quot; the request:</p>
  <pre><code>@app.after_request
  def add_header(response):
      response.headers['Access-Control-Allow-Origin'] = '*'
      return response
  </code></pre>
  <p>No dice. I get the same error. Is there a way to just set the response headers in the route function? Something like this would be ideal:</p>
  <pre><code>@app.route('/hello', methods=[&quot;POST&quot;])
      def hello(response): # is this a thing??
          response.headers['Access-Control-Allow-Origin'] = '*'
          return response
  </code></pre>
  <p>but I cant find anyway to do this. Please help.</p>
  <p><strong>EDIT</strong></p>
  <p>if I curl the url with a POST request like so:</p>
  <pre><code>curl -iX POST http://localhost:5000/hello
  </code></pre>
  <p>I get this response:</p>
  <pre><code>HTTP/1.0 500 INTERNAL SERVER ERROR
  Content-Type: text/html
  Content-Length: 291
  Server: Werkzeug/0.9.6 Python/2.7.6
  Date: Tue, 16 Sep 2014 03:58:42 GMT
  
  &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 3.2 Final//EN&quot;&gt;
  &lt;title&gt;500 Internal Server Error&lt;/title&gt;
  &lt;h1&gt;Internal Server Error&lt;/h1&gt;
  &lt;p&gt;The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.&lt;/p&gt;
  </code></pre>
  <p>Any ideas?</p>