rubyfiltersinatra

Before-filter for all POST requests in Sinatra?


Is there a way to create a "before" filter to capture and pre-process all POST requests in Sinatra?


Solution

  • One way of doing this would be to create a custom condition to use in the filter:

    set(:method) do |method|
      method = method.to_s.upcase
      condition { request.request_method == method }
    end
    
    before :method => :post do
      puts "pre-process POST"
    end