Set default content_type for Sinatra

ma11hew28 picture ma11hew28 · Jan 8, 2011 · Viewed 22.9k times · Source

In Sinatra, is it possible to make content_type 'application/json' the default? cause I'm writing an api.

Answer

Adam Lassek picture Adam Lassek · Jan 8, 2011

Sure, add content_type to the before callback:

class MyApp < Sinatra::Base

  before do
    content_type 'application/json'
  end

  ...

end

Sinatra 1.1 introduces pattern-matching before filters:

before '/admin/*' do
  check_logged_in
end