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

About Wondering Bear
39 years old male bear.

One Response to Sinatra request headers helper

  1. Schneider says:

    Cool šŸ™‚
    And which font are you using in that site (for the posts)?

    Cheers,
    Felipe.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: