Rails: Middleware: Remove header from response
Sometimes it is desirable to suppress headers in a response. It is convenient to use rack middleware to accomplish this task. The class we will use for this is called HeaderDelete. Make an entry in application.rb for this class to be called before rack middleware:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'csv'
Bundler.require(:default, Rails.env)
module SampleApp
class Application < Rails::Application
config.middleware.insert_before Rack::Runtime, "HeaderDelete"
end
end
The class itself is self expanatory regarding which headers are targeted for removal:
class HeaderDelete
def initialize(app, options = {})
@app, @options = app, options
end
def call(env)
r = @app.call(env)
#[status, headers, response] = r
r[1].delete "X-Runtime"
r[1].delete "X-Powered-By"
r[1].delete "Server"
r
end
end
Comment