How can I access HTTP Headers?

517 views
Skip to first unread message

James M.

unread,
Jan 2, 2012, 11:31:49 PM1/2/12
to web2py-users
I need to get a request header that is being sent with an http post.
I thought I could access it with something like:
request.headers['HTTP_SOME_HEADER']
but it doesn't work... I looked at all request attributes and don't
see the headers.
Any idea how to get it?

lyn2py

unread,
Jan 3, 2012, 12:17:48 AM1/3/12
to web2py-users
What headers are you specifically looking for that are not available?

Have you looked under request.env, e.g. request.env.http_referer (and
other headers like http_host, http_user_agent)?

Anthony

unread,
Jan 3, 2012, 12:26:29 AM1/3/12
to web...@googlegroups.com
I think the 'Your-Header' header should be accessible via:

request.env.http_your_header

or

request.wsgi.environ.HTTP_YOUR_HEADER

Anthony

James M.

unread,
Jan 3, 2012, 1:54:17 AM1/3/12
to web2py-users
I am using twilio. When twilio posts to the url I supplied, they sign
the request with a customer HTTP header X-Twilio-Signature, which I
can use to validate that it is actually twilio making the request.

James M.

unread,
Jan 3, 2012, 1:57:21 AM1/3/12
to web2py-users
I looked into those two suggestions, the header doesn't seem to be
available.

I am starting to think that custom headers aren't passed to web2py...

Massimo Di Pierro

unread,
Jan 3, 2012, 9:37:17 AM1/3/12
to web2py-users
If the header is not is not in request.env than it was never there or
wsgi is not passing it along.

Anthony

unread,
Jan 3, 2012, 10:06:43 AM1/3/12
to web...@googlegroups.com
Can you confirm that the header is being sent? I just mocked up an Ajax request with an 'X-Twilio-Signature' header, and found it in web2py as request.env.http_x_twilio_signature. Try including response.toolbar() in your view (which is included by default in generic.html for local requests) and look at the contents of the request object to see what headers were received (all the HTTP headers start with request.env.http_, and are repeated as request.wsgi.environ.HTTP_).

Anthony


On Tuesday, January 3, 2012 1:57:21 AM UTC-5, James M. wrote:
I looked into those two suggestions, the header doesn't seem to be
available.

I am starting to think that custom headers aren't passed to web2py...

James M.

unread,
Jan 3, 2012, 12:31:10 PM1/3/12
to web2py-users
I am testing it using http://hurl.it and manually adding the header,
so I am fairly confident the header is getting passed(at least in
testing). What Massimo suggested, wsgi not passing it, could be the
issue. I have a pretty vanilla nginx install on linode. My install
pretty much followed this script
http://code.google.com/p/web2py/source/browse/scripts/setup-web2py-nginx-uwsgi-ubuntu.sh.

I will dig into the wsgi config on nginx, maybe the default config
doesn't pass custom headers.

Anthony

unread,
Jan 3, 2012, 1:01:57 PM1/3/12
to web...@googlegroups.com
On Tuesday, January 3, 2012 12:31:10 PM UTC-5, James M. wrote:
I am testing it using http://hurl.it and manually adding the header,
so I am fairly confident the header is getting passed(at least in
testing).

Hmm, when I try hurl.it in IE and add a custom header, the custom header doesn't appear in the request headers in IE's developer tools. Instead, I see the following added to the query string of the URL: header-keys%5B%5D=X-Twilio-Signature. Maybe the problem is with hurl.it.

Anthony 

James M.

unread,
Jan 3, 2012, 5:01:01 PM1/3/12
to web2py-users
I think you are looking at the post from IE to http://hurl.it, which
is including the Header parameter (as a post parameter) to hurl.it,
when it arrives at hurl.it it should create a http post from the
hurl.it server to the url specified with header, parameters and body
specified.

On Jan 3, 10:01 am, Anthony <abasta...@gmail.com> wrote:
> On Tuesday, January 3, 2012 12:31:10 PM UTC-5, James M. wrote:
>
> > I am testing it usinghttp://hurl.itand manually adding the header,

Anthony

unread,
Jan 3, 2012, 5:17:52 PM1/3/12
to web...@googlegroups.com
Sorry, not enough sleep last night. Anyway, I have been able to pick up custom headers when running Rocket and Apache with mod_wsgi, so it should work in general. I'm not sure what the problem is in this case. Have you tried actual requests from Twilio (rather than just testing with hurl.it)?

Anthony


On Tuesday, January 3, 2012 5:01:01 PM UTC-5, James M. wrote:
I think you are looking at the post from IE to http://hurl.it, which
is including the Header parameter (as a post parameter) to hurl.it,
when it arrives at hurl.it it should create a http post from the
hurl.it server to the url specified with header, parameters and body
specified.

Anthony

unread,
Jan 3, 2012, 5:26:31 PM1/3/12
to web...@googlegroups.com
Actually, just tried sending a hurl.it request, and Apache with mod_wsgi did pass the custom header to web2py, so maybe your problem is with your nginx setup.

Anthony


On Tuesday, January 3, 2012 5:17:52 PM UTC-5, Anthony wrote:
Sorry, not enough sleep last night. Anyway, I have been able to pick up custom headers when running Rocket and Apache with mod_wsgi, so it should work in general. I'm not sure what the problem is in this case. Have you tried actual requests from Twilio (rather than just testing with hurl.it)?

Anthony

On Tuesday, January 3, 2012 5:01:01 PM UTC-5, James M. wrote:
I think you are looking at the post from IE to http://hurl.it, which
is including the Header parameter (as a post parameter) to hurl.it,
when it arrives at hurl.it it should create a http post from the
hurl.it server to the url specified with header, parameters and body
specified.

James M.

unread,
Jan 3, 2012, 5:50:03 PM1/3/12
to web2py-users
Good to know that it is working with apache. Thanks for your help.

On Jan 3, 2:26 pm, Anthony <abasta...@gmail.com> wrote:
> Actually, just tried sending a hurl.it request, and Apache with mod_wsgi
> did pass the custom header to web2py, so maybe your problem is with your
> nginx setup.
>
> Anthony
>
>
>
>
>
>
>
> On Tuesday, January 3, 2012 5:17:52 PM UTC-5, Anthony wrote:
>
> > Sorry, not enough sleep last night. Anyway, I have been able to pick up
> > custom headers when running Rocket and Apache with mod_wsgi, so it should
> > work in general. I'm not sure what the problem is in this case. Have you
> > tried actual requests from Twilio (rather than just testing with hurl.it)?
>
> > Anthony
>
> > On Tuesday, January 3, 2012 5:01:01 PM UTC-5, James M. wrote:
>
> >> I think you are looking at the post from IE tohttp://hurl.it, which
> >> is including the Header parameter (as a post parameter) to hurl.it,
> >> when it arrives at hurl.it it should create a http post from the
> >> hurl.it server to the url specified with header, parameters and body
> >> specified.
>
> >> On Jan 3, 10:01 am, Anthony <aba...@gmail.com> wrote:
> >> > On Tuesday, January 3, 2012 12:31:10 PM UTC-5, James M. wrote:
>
> >> > > I am testing it usinghttp://hurl.itandmanually adding the header,

Anthony

unread,
Jan 3, 2012, 9:26:07 PM1/3/12
to web...@googlegroups.com
Note, web2py Ajax components send some custom headers (web2py-component-element and web2py-component-location), so if custom headers aren't getting through in a particular configuration, I suppose Ajax components won't work properly either. Let us know what you find out.

Anthony


On Tuesday, January 3, 2012 5:50:03 PM UTC-5, James M. wrote:
Good to know that it is working with apache.  Thanks for your help.

Reply all
Reply to author
Forward
0 new messages