rails-api & rails 4 not working with rack-cors?
See original GitHub issueI’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.
Issue Analytics
- State:
- Created 10 years ago
- Comments:90 (24 by maintainers)
Top Results From Across the Web
POST Method not working in rack-cors rails 4 - Stack Overflow
All the GET method in my project is working fine, but POST method returns 500 internal server error.
Read more >Rails CORS Guide: What It Is and How to Enable It - StackHawk
Cross-origin resource sharing (CORS) is a great security mechanism that every web application developer should know about.
Read more >Handling CORS issues in your Rails API | by Paul Nicholson
For anyone unaware, CORS refers to 'Cross origin resource sharing' and it is essentially how the server filters out requests from an origin ......
Read more >Rails API & CORS. A dash of consciousness - The Codest
We will be still using rack-cors (like we were told to) – but our way. Let's use 2 ENV variables: ALLOWED_ORIGINS for literal...
Read more >Setting Up CORS On A Rails App - DEV Community
Problem. I was trying to integrate a Rails API with a React front end ... a Medium article that requires the use of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+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.0
Darwin 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_64
Please 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 do
and enableconfig.serve_static_assets
in production.rb?It works, but I really don’t want to enable static assets 😞