龙卷风requestHandler:如何在获得参数之前解码URL编码查询

发布于 2025-02-13 23:35:30 字数 631 浏览 3 评论 0原文

我正在使用Python Tornado框架来构建API和请求Handler来处理我通过get()方法获得的查询。

我的处理程序

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
        ......

问题是,当查询被编码时,例如“文本%3DHi%26lang%3DEN”,请求Handler不会解码查询并识别其两个参数。相反,它将“文本”参数值标识为“ hi& lang = en'。

那么,如果编码URL,我该如何阅读论点呢?显然requesthandler.get_arguments()似乎在解析URL参数之前似乎没有解码它们

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2025-02-20 23:35:30

由于&字符也按照HTTP规格进行编码,因此将其视为值的一部分,而不是定界符。

实际上,整个字符串 - text%3DHi%26lang%3DEN应该被视为一个值,因为分界符(=&)也已编码。

基本上,当您发送请求时,您只需要编码参数值,而不是整个查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文