Sinatra request headers helper

It is pretty simple, but here is an example of getting request headers from the Rack Environment in a Sinatra application :

require 'rubygems'
require 'sinatra'

helpers do
  def request_headers
    env.inject({}){|acc, (k,v)| acc[$1.downcase] = v if k =~ /^http_(.*)/i; acc}
  end  
end

get '/headers' do
  puts request_headers.inspect
 end

request_headers helper will filter all HTTP headers and downcase names.

Advertisements