Sinatra OPTIONS HTTP Verb

2019-01-14 13:12发布

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

标签: sinatra
4条回答
Animai°情兽
2楼-- · 2019-01-14 13:49

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.

查看更多
Evening l夕情丶
3楼-- · 2019-01-14 13:51

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-14 13:56
叛逆
5楼-- · 2019-01-14 13:58

the answer is, simply, yes! (look under Routes in the read me http://www.sinatrarb.com/intro.html)

查看更多
登录 后发表回答