How to access request headers

4,747 views
Skip to first unread message

mydoghasworms

unread,
Nov 26, 2009, 1:56:16 AM11/26/09
to sinatrarb
Sorry for this newbie question, but I can't find the answer in the
documentation:

How do I access the HTTP request headers in my sinatra app? The
request method seems to give access only to response headers.

Ryan Tomayko

unread,
Nov 29, 2009, 9:34:55 AM11/29/09
to sina...@googlegroups.com
The request or env objects give access to request headers:

request.env['HTTP_USER_AGENT']
env['HTTP_USER_AGENT']

See the Rack spec for more information on what can be included in the env Hash:

http://rack.rubyforge.org/doc/SPEC.html

The response objects gives access to response headers:

response['Content-Type'] = 'text/plain'

Thanks,
Ryan
--
# tomayko.com/about

mydoghasworms

unread,
Nov 30, 2009, 12:49:30 AM11/30/09
to sinatrarb
Thanks Ryan, that did the trick.

Giorgio Robino

unread,
Nov 28, 2014, 6:12:53 AM11/28/14
to sina...@googlegroups.com
in facts
I just discovered a 'trick'...

if a param in header is 'MYPARAM'

I can access it with env['HTTP_MYPARAM']

... so Sinatra 1.4.5 seem to add the "HTTP_" prefix ... ?!

strange.
giorgio

Iain Barnett

unread,
Dec 20, 2014, 12:03:49 AM12/20/14
to sina...@googlegroups.com

On 28 Nov 2014, at 11:12, Giorgio Robino <giorgio...@gmail.com> wrote:

in facts
I just discovered a 'trick'...

if a param in header is 'MYPARAM'

I can access it with env['HTTP_MYPARAM']

... so Sinatra 1.4.5 seem to add the "HTTP_" prefix ... ?!

strange.
giorgio


That’s part of the Rack spec. See http://www.rubydoc.info/github/rack/rack/file/SPEC under  HTTP_ Variables.

Regards,
iain
signature.asc
Reply all
Reply to author
Forward
0 new messages