API Key/Token for REST API

173 views
Skip to first unread message

mvl

unread,
Feb 8, 2012, 6:22:23 AM2/8/12
to web.py
Hello,

I'am searching for a way to authenticate against the api that I have
written with web.py.

I thought about something like a API-Key/Token that would be included
in the HTTP headers of the request.

How can I get a specific HTTP-Header with web.py? I'am using
modpython_gateway.py/mod_python in my production environment.

I have seen the method web.header() - but it seems like that this
method is only for setting specific response-headers.

Ben Corneau

unread,
Feb 8, 2012, 10:19:26 PM2/8/12
to web.py
You can access the headers using web.ctx.envrion. Your headers will be
an item in the envrion dict prepended with "HTTP_" and converted to
upper case.
I'm not familiar with modpython_gateway.py and don't know if it will
change this behavior.

example request (I've recently discovered requests,
http://docs.python-requests.org/en/latest/index.html. It's awesome!)
---------------------------------
import requests
requests.get("http://127.0.0.1:8080/resource",
headers={"auth_token":"xxxxxxxxxxx"})

web.py code
---------------------------
    def GET(self):
        auth_token = web.ctx.environ["HTTP_AUTH_TOKEN"]

mvl

unread,
Feb 9, 2012, 4:10:34 AM2/9/12
to web.py
The result of trying this code:
Traceback (most recent call last):

File "/usr/lib64/python2.6/site-packages/mod_python/importer.py",
line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)

File "/usr/lib64/python2.6/site-packages/mod_python/importer.py",
line 1229, in _process_target
result = _execute_target(config, req, object, arg)

File "/usr/lib64/python2.6/site-packages/mod_python/importer.py",
line 1128, in _execute_target
result = object(arg)

File "/usr/lib64/python2.6/wsgiref/modpython_gateway.py", line 205,
in handler
module = __import__(modname, globals(), locals(), [''])

File "/www/path/service/serverw.py", line 10, in <module>
web.header('Content-Type', 'text/plain')

File "/usr/local/lib/python2.6/site-packages/web/webapi.py", line
222, in header
ctx.headers.append((hdr, value))

AttributeError: 'ThreadedDict' object has no attribute 'headers'


On 9 Feb., 04:19, Ben Corneau <bencorn...@gmail.com> wrote:
> You can access the headers using web.ctx.envrion. Your headers will be
> an item in the envrion dict prepended with "HTTP_" and converted to
> upper case.
> I'm not familiar with modpython_gateway.py and don't know if it will
> change this behavior.
>
> example request  (I've recently discovered requests,http://docs.python-requests.org/en/latest/index.html. It's awesome!)

mvl

unread,
Feb 9, 2012, 9:36:41 AM2/9/12
to web.py
Thanks, this works!

On 9 Feb., 04:19, Ben Corneau <bencorn...@gmail.com> wrote:
> You can access the headers using web.ctx.envrion. Your headers will be
> an item in the envrion dict prepended with "HTTP_" and converted to
> upper case.
> I'm not familiar with modpython_gateway.py and don't know if it will
> change this behavior.
>
> example request  (I've recently discovered requests,http://docs.python-requests.org/en/latest/index.html. It's awesome!)
Reply all
Reply to author
Forward
0 new messages