Comments (5)
Yeah, you can use something like this to add OPTIONS
support for your route:
match :foo, via: [:get, :options], controller: 'users'
(I know it was 8 months ago but still)
from rack-cors.
@dfl Here is how I got it to work in Rails 4.0:
# config/application.rb
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :put, :patch, :post]
end
end
# config/routes.rb
match '/locations' => 'locations#render_204', via: [:options]
match '/locations/:id' => 'locations#render_204', via: [:options]
# app/controllers/locations_controller.rb
def render_204
head 204
end
With that in place, I can now issue an OPTIONS request like so:
curl -i http://www.lvh.me:8080/api/locations/1 -H "Origin: http://example.com" -X OPTIONS
And I get the correct headers in the response:
Access-Control-Allow-Origin: http://example.com
Access-Control-Allow-Methods: GET, PUT, PATCH, POST
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000
Access-Control-Allow-Credentials: true
If you have many resources and routes that need to be able to respond to an OPTIONS request, you can create a separate controller that just defines the render_404
method. For example:
# app/controllers/cors_controller.rb
class CorsController < ApplicationController
def render_204
head 204
end
end
Then send all requests to that controller:
# config/routes.rb
match '*foo' => 'cors#render_204', via: [:options]
@cyu Does this look right to you? If so, I think it should be documented in this repo as it's not obvious to CORS beginners. I'd be happy to submit a PR for a README update, or I can add a Wiki page.
from rack-cors.
@monfresh this looks great - yes a PR would be great!
from rack-cors.
This issues is a duplicate of #44
from rack-cors.
Ok, reviewing this issue in context #57, there does seem to be an issue here, but it's not what you'd expect.
The issue as I see it is that non-preflight OPTIONS
requests are returning CORS headers when they shouldn't be. The curl command @monfresh used in his example isn't a valid Preflight request (it's missing the Access-Control-Request-Method
header which is required for Preflight). That request should be treated as a non-CORS request and not return those headers at all.
I'm going to create a new issue and close this one.
from rack-cors.
Related Issues (20)
- the Vary header does not seem to be set for CORS pre-flight checks that use the OPTIONS method HOT 1
- Controller still executes the request although CORS is blocked for this source HOT 1
- Support for Timing-Allow-Origin HOT 5
- Tag for v2.0.0 is missing HOT 3
- Does not work with Rack 3.0.0 HOT 1
- My CORS setup blocks bad urls but not POSTMAN requests HOT 3
- Vulnerabilities in included mocha.js HOT 1
- Stripe api with Cloud9 setup
- Is there any CI? HOT 1
- Cookie session related middleware interferes HOT 3
- Remove/Replace Rack::Utils::HeaderHash HOT 7
- delete the confusing `2.0.0` tag since `v2.0.0` exists? HOT 1
- issue comeback duplicate headers because of header name case HOT 3
- Release version 2.0.1 HOT 2
- How to add Access-Control-Allow-Private-Network header in Option response?
- Why is Rails.application.config.hosts required? HOT 1
- Regex origin woes HOT 2
- Upgrade to rails 7.1.1
- Ressource with $ are not handled as expected
- Access-Control-Allow-Origin is returned only if correct Origin header is available
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rack-cors.