title: Heroku, Rails 4, and Rack::Cors
body: <p>I am trying to use Rack::Cors with my Rails 4 application so that I can do a JSON based API.</p>
  
  <p>CORS is in my Gemfile like this:</p>
  
  <pre><code>gem 'rack-cors', :require =&gt; 'rack/cors'
  </code></pre>
  
  <p>I am doing the configuration in my application.rb file like this:</p>
  
  <pre><code>config.middleware.insert_after Rails::Rack::Logger, Rack::Cors, :debug =&gt; true, :logger =&gt; Rails.logger do
      allow do
          origins '*'
          resource '/messages*', :headers =&gt; :any, :methods =&gt; [:post, :options]
      end
  end
  </code></pre>
  
  <p>I am inserting after <code>Rails::Rack::Logger</code> in an attempt to get debugging information.</p>
  
  <p>I am using CURL to test it, here is what I have been running:</p>
  
  <pre><code>curl --verbose --request OPTIONS http://jasonbutzinfo.herokuapp.com/messages.json --header 'Origin: http://www.jasonbutz.info' --header 'Access-Control-Request-Headers: Origin, Accept, Content-Type' --header 'Access-Control-Request-Method: POST'
  </code></pre>
  
  <p>When I run the rails app on my local machine it works without issue. When I hit the Heroku app this is what I get:</p>
  
  <pre><code>&gt; OPTIONS /messages.json HTTP/1.1
  &gt; User-Agent: curl/7.30.0
  &gt; Host: jasonbutzinfo.herokuapp.com
  &gt; Accept: */*
  &gt; Origin: http://www.jasonbutz.info
  &gt; Access-Control-Request-Headers: Origin, Accept, Content-Type
  &gt; Access-Control-Request-Method: POST
  &gt; 
  * Empty reply from server
  * Connection #0 to host jasonbutzinfo.herokuapp.com left intact
  curl: (52) Empty reply from server
  </code></pre>
  
  <p>I did find this question (<a href="https://stackoverflow.com/questions/18538549/cant-get-rack-cors-working-in-rails-application">Can&#39;t get rack-cors working in rails application</a>), but there wasn't any helpful answer provided.</p>
  
  <p><strong>Update 11/13/2013 16:40 EST</strong></p>
  
  <p>I've been trying to do some more debugging with what is going on. I have monkey patched a few of Rack::Cors' methods to see if they are even being called on Heroku. I have also changed where I insert Cors to be at the top of the rack middleware stack.</p>
  
  <p>With my monkey patching I have put <code>puts</code> statements in the <code>initialize</code>, <code>call</code>, and <code>allow</code> methods. The <code>initialize</code> and <code>allow</code> methods are both called. The <code>call</code> method is never called. So it seems there is something that is stopping the request before it gets to the cors middleware.</p>