Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra OPTIONS HTTP Verb

Tags:

sinatra

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
like image 660
Kevin Sylvestre Avatar asked Dec 04 '10 04:12

Kevin Sylvestre


3 Answers

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.

like image 102
Kevin Sylvestre Avatar answered Nov 14 '22 04:11

Kevin Sylvestre


Yes, already does it Sinatra Routes documentation

like image 6
fguillen Avatar answered Nov 14 '22 04:11

fguillen


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.

like image 2
jergason Avatar answered Nov 14 '22 05:11

jergason