Does Sinatra support the OPTIONS HTTP verb? Something like:
options '/' do
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST"
halt 200
end
After a bit of hacking I managed to get it working using:
before do
if request.request_method == 'OPTIONS'
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST"
halt 200
end
end
Edit:
After some more looking around on this issue, I realized that a PULL request is up on GitHub for the addition of the OPTIONS
verb (https://github.com/sinatra/sinatra/pull/129). I took the solution and hacked it in using the following snippet:
configure do
class << Sinatra::Base
def options(path, opts={}, &block)
route 'OPTIONS', path, opts, &block
end
end
Sinatra::Delegator.delegate :options
end
Now I can simply use:
options '/' do
...
end
Edit:
The pull request should be merged. No more need for the hack.
Yes, already does it Sinatra Routes documentation
No it does not. If you look at the code on GitHub you can see where the HTTP verbs are defined, and options
is not one of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With