开发者

Sinatra OPTIONS HTTP Verb

Does Sinatra support the OPTIONS HTTP verb? Something like:

options '/' do
  response.headers["Access-Control-Allow-Origin"] = "*"
  response.headers["Acces开发者_如何学运维s-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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜