rack-cors: rails-api & rails 4 not working with rack-cors?
I’ve added rack-cors to my gemfile and put in my development.rb and production.rb (I also tried application.rb):
config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
allow do
origins '*.example.com'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
When I run rake middleware, I can see Rack::Cors. When I do a curl request for development or production no CORS headers are added to the requests. There’s no errors to try to track, so I’m at a loss for why this isn’t working.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 90 (24 by maintainers)
Commits related to this issue
- I'm terrible and putting CORS here — committed to nickjs/rack-zippy by nickjs 10 years ago
- Attempt fix to rack-cors for rails 4 per: https://github.com/cyu/rack-cors/issues/33 (why is this necessary? CORS was working before.) — committed to hyperstudio/artbot-api by jamiefolsom 10 years ago
- Move rack-cors to the top of the middleware stack According to cyu/rack-cors#33 it needs to at the top of the middleware stack for recent versions of Rails 4 — committed to zooniverse/panoptes by deleted user 9 years ago
- Move Rack::Cors to the top of the middleware stack https://github.com/cyu/rack-cors/issues/33 says that's what should be done — committed to NYULibraries/eshelf by scotdalton 9 years ago
+1 from me.
Using rack-cors as per the readme in a rails-api project and I see no headers added by the gem if I do a GET. If I do a POST, I can get the headers properly when I tested using the Chrome Postman app.
Code for middleware insertion (copied verbatim from README):
All headers returned:
Environment information:
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]Rails 4.0.0Darwin A-strong-preference-for-raincoats.local 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64Please let me know if I can provide any additional information. Thanks!
So, to summarize: currently the only way to fix this is to use
config.middleware.insert_before ActionDispatch::Static, Rack::Cors doand enableconfig.serve_static_assetsin production.rb?It works, but I really don’t want to enable static assets 😞
I have same problem, but only when running in production mode. For development it works OK. I’m using vanilla Rails 4.
Edit: Ah, I needed to use
config.middleware.insert_before ActionDispatch::Static, Rack::Cors doand this to work you need to setconfig.serve_static_assets = trueinproduction.rb.I think I met this problem in Rails 5.
Versions:
Server is running under development env.
Results come first, as shown in the picture, I tried two requests with different origins, and the server gave me almost the same response(only with little difference in the headers).
Server SHOULD give 401, right?
Here’s my config:
Here’re middlewares:
I ran into this problem, turned out it was because I was testing my api with cURL or by hitting the api directly in chrome. In both cases there is no Origin header on the request. Adding the Origin header to my request triggered the response headers to include
Access-Control-Allow-Origin: http://127.0.0.1:4200and etc.I donno, it’s working for me on Heroku with Rails 4.2.0.beta2, when I configure CORS in a Rackup file, as below.
@thebravoman glad that was it.
Looking at the spec, it looks like the Origin header format is
<scheme> "://" <hostname> [ ":" <port> ]. Maybe I should automatically strip out any starting slash by default.@zigomir’s solution works for me, though I tweaked it and I am using
config.middleware.insert_after Rails::Rack::Logger, Rack::Cors, :logger => Rails.logger doso that I can get log messages. Has anyone managed to get this working on Heroku?