Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado: get request arguments

I have following code:

application = tornado.web.Application([
    (r"/get(.*)", GetHandler),
])

class GetHandler(tornado.web.RequestHandler):
    def get(self, key):
        response = {'key': key}
        self.write(response)

when I go to localhost:port/get?key=python I receive empty key value {'key': ''}. What is wrong here?

like image 384
Rudziankoŭ Avatar asked Nov 13 '17 17:11

Rudziankoŭ


1 Answers

(.*) in regex matches everything. So this — (r"/get(.*)", GetHandler) — will match anything followed by /get, example:

/get
/getsomething
/get/something
/get.asldfkj%5E&(*&fkasljf

Let's say a request comes in at localhost:port/get/something, then the value of key argument in GetHandler.get(self, key) will be /something (yes, including the slash because .* matches everything).

But if a request comes in at localhost:port/get?key=python, the value of key argument in GETHandler.get(self, key) will be an empty string. It happens because the part containing ?key=python is called a Query String. It's not part of the url path. Tornado (or almost every other web framework) doesn't pass this to the view as an argument.


There are two ways you can change your code:

  1. If you want to access your view like this - localhost:port/get?key=python, you'll need to make changes to your url config and to your view:

    application = tornado.web.Application([
        (r"/get", GetHandler),
    ])
    
    class GetHandler(tornado.web.RequestHandler):
        def get(self):
            key = self.get_argument('key', None)
            response = {'key': key}
            self.write(response)
    
  2. If you don't want to change your app url config and your view, you'll need to make the request like this - localhost:port/get/python.
    But still you'll need to make a small change to your url config. Add a slash - / - between get and (.*), because otherwise the value of key would be /python instead of python.

    application = tornado.web.Application([
        (r"/get/(.*)", GetHandler), # note the slash
    ])
    
like image 101
xyres Avatar answered Nov 01 '22 13:11

xyres