Set default content_type for Sinatra Set default content_type for Sinatra ruby ruby

Set default content_type for Sinatra


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_inend


For a JSON API the most recommendable way to set a default Content-Type for all your responses is to add the following in your Sinatra class:

set :default_content_type, :json

It will include a Content-Type: application/json header in all your responses.