-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rails-api & rails 4 not working with rack-cors? #33
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): config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end All headers returned:
Environment information:
Please let me know if I can provide any additional information. Thanks! |
I have same problem, but only when running in production mode. For development it works OK. I'm using vanilla Rails 4. Edit: |
Thanks @zigomir, that did it for me in Rails 4! |
Master works for me |
@zigomir's solution works for me, though I tweaked it and I am using |
I have had it working off and on, but I can't seem to isolate what causes it to fail and succeed. |
i use the same solution as @jbutz too and seems to work for me too |
Should we change the README to insert_after ? |
Using |
It should be definitely set in README - I searched the whole internet until I found a fix for that .. |
Thanks @zigomir that did it for me as well, although without static assets (since we're not serving them from Rails). |
This worked for me thanks, was driving me mental |
I used |
So, to summarize: currently the only way to fix this is to use It works, but I really don't want to enable static assets 😞 |
You shouldn't need to set |
…is necessary? CORS was working before.)
@cyu Any idea why this isn't working in rails 4? The "solution" stopped working for me recently (as well as others too from what I can see on the interwebs). |
I create an example in |
I copied the code verbatim from your |
@visoft Did you manage to solve this issue? I'm using standard Rails 4.1.2, but can't get it to work either. |
@simonbogarde, I haven't. I hacked my |
@visoft Thanks. When I manage to fix this, I'll post the solution here. |
I am also now having the same "No 'Access-Control-Allow-Origin' header is present on the requested resource" issue with rails 4.1.5 when using
and also
|
i had this working on heroku about a year ago, then several months later, it stopped working, but only on heroku. before, it seemed to process my rackup file, but now it's not. just now looking into it. |
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.
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
cors_origins = ENV.fetch('RAILS42_CORS_ORIGINS', '*')
require 'rack/cors'
use Rack::Cors do
# TODO: secure
allow do
origins cors_origins
resource '*',
:headers => :any,
:expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
:methods => [:get, :post, :options, :delete, :put]
end
end |
I have the same issue on Rails 4.0.8, no additional headers being supplied in HTTP get requests after following the installation instructions. |
I'm chiming in to say that I also have this issue after following the install instructions. I've also tried adding @dcunited001's code snippet to my |
@cyu It's not blank, I deleted it. The request url was:
Which is the specified Rack::Cors url. On top of that, I've tried enabling debug mode which did output headers of the request. The headers however were blank. |
@cyu Server debug output is:
A temporary work around for this is to add the following into your controller:
Modify the headers to be specific to your situation, but that should do the job. |
@zeeshan-m with debug turned on in the middleware, you sure be getting a X-Rack-Cors header returned to your browser. What does that value look like? Also, in your first post, you're getting a error message where the Origin is from port 8080, but it seems like most of your recent examples are from port 8100. Why is that? |
@cyu I was testing the issue with the different apps that we have that make api calls, they run on different ports so thats most likely why you saw the 8080 vs 8100 issue. Full response headers in debug mode are:
|
@yonkeltron Did you find any way out for this? |
@zeeshan-m weird – that |
@cyu Any idea why the headers are not coming for GET request ? |
@23ranjan what version of rails and rails-api are you running? What does your middleware stack look like ( |
@cyu I'm having this issue as well. I can do a post request, but no Access-Control headers are sent with get requests. I'm running rails 5.0.2. My middleware stack:
The value the X-Rack-CORS header is "miss; no-origin". |
@jtibbertsma How are you testing? The header suggests that the user agent isn't providing an |
I'm using Postman |
Ok, it's working from localhost now, I just had to fix my settings :) |
@jtibbertsma ok – FYI Postman doesn't send the required |
It works for post requests tho |
I came across a similar issue: OPTIONS requests being sent from my Angular front end weren't getting CORS headers attached by rack-cors. Setting Seems like possibly because I was missing these two headers (which were in the OPTIONS request but not the other requests: |
No Access-Control-Allow-Origin on my side also. This is the configuration
This is the curl request for a test
This is the output in the log on the server side:
This is the curl response
It is not setting the Access-Controll-Allow-Origin |
@thebravoman the |
@thebravoman sorry Since your origin is 'https://mysite.com/', that does an exact string match with the |
@cyu Thanks. This seems to be the problem. Quite funny. I spent a lot of time on this. |
@thebravoman glad that was it. Looking at the spec, it looks like the Origin header format is |
If the spec allows it, it would be great.
I spend an awful amount of time looking at this and not seeing it.
…On 6 February 2018 at 18:09, Calvin Yu ***@***.***> wrote:
@thebravoman <https://github.com/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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#33 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABQDBQw2R7fO534zRgE5-CwdwRgQ5OfCks5tSHlGgaJpZM4BEaSr>
.
--
+359 878 93 30 94
Училище по роботика "Robopartans"
www.robopartans.com
|
I was able to fix the error locally and on Heroku. I was getting a 401 when making a GET request through the browser. I was able to make progress in figuring out the problem by making the same request using Postman. That showed me the body of the response which wasn't visible in the log when debug mode was on: Seeing an unexpected message about authentication helped me realize that I had a typo in my URL ( I'm writing this explanation, because people can also get in this same situation of receiving 401s and thinking it's related to rack-cors if they are using Devise and they are requesting a route protected by This is what worked for me in application.rb:
|
Has there been any fix or workaround for this error? I am having a similar error where I am getting No route (404) error when making an OPTIONS requests to any of my endpoints. I am using rails v6, rack-cors ~>1.0 in my application.rb I have
|
@jattoabdul how are you testing this? Are you sure your requests are passing in an |
Same issue here :( |
I'm closing this issue because there are different issues at play (some legit, some invalid). If you're still having problems. Please create a new issue and provide the specifics. |
Has there been a fix or workaround for this? |
I've added rack-cors to my gemfile and put in my development.rb and production.rb (I also tried application.rb):
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.
The text was updated successfully, but these errors were encountered: