tejeshwar
tejeshwar

Reputation: 31

Tornado RequestHandler: How to decode url encoded query before getting the arguments

I'm using the python Tornado framework to build an API and RequestHandler to handle the queries I get through the get() method.

My handler

handlers = [
    (r"?", MainHandler)
]
class MainHandler(RequestHandler):
    
    def get(self):
        args = {
            "text": RequestHandler.get_argument(self, "text", default=""),
            "lang": RequestHandler.get_argument(self, "lang", default=""),
        }
        # Process the query
        ......

The issue is when the query is URL encoded like 'text%3DHi%26lang%3Den', the RequestHandler doesn't decode the query and identify its two parameters. Instead, it identifies the 'text' parameter value as 'Hi&lang=en'.

So, how do I read the arguments if they are URL encoded? Apparently RequestHandler.get_arguments() doesn't seem to be decoding them before parsing URL parameters

Upvotes: 3

Views: 261

Answers (1)

xyres
xyres

Reputation: 21744

Because the & character is also encoded, as per HTTP spec, it is treated as part of a the value, not as a delimiter.

In fact, this whole string - text%3DHi%26lang%3Den should be considered a single value because the delimiters (= and &) are also encoded.

Basically, when you're sending the request you only have to encode the argument values, not the whole querystring.

Upvotes: 1

Related Questions