De-encode URL parameters

2019-02-18 02:34发布

I am talking to a server that used to send me HTTP strings like this:

/path/to/my/handler/?action-query&id=112&type=vca&info=ch=0&type=event&ev16[sts=begin (...)

So the "info" GET parameter included "=" and "&" characters. It was rather unorthodox but nevertheless we wrote a parser for it. However, recently they have decided to encode part of it, so now the string looks like this..

/path/to/my/handler/?action=query&id=112&type=vca&info=ch%3D0%26type%3Devent%26ev46[sts%3Dbegin (...)

This breaks our parser, which expects a string like the first one.

Can I somehow "de-encode" the string, so that I can use the old code (so that it's not broken as we re-write the parser)?

As per answer below, we can use urllib.unquote() to clean the string up. However, we are relying on request.GET, which gets set up based on the first string. Is it possible to reconstruct the GET object based on the new converted string, or somehow force it to re-evaluate?

1条回答
欢心
2楼-- · 2019-02-18 02:44

I suspect what you want is the unquote function from the urllib module.

>>> s = '/path/to/my/handler/?action=query&id=112&type=vca&info=ch%3D0%26type%3Devent%26ev46[sts%3Dbegin'
>>> import urllib
>>> urllib.unquote(s)
'/path/to/my/handler/?action=query&id=112&type=vca&info=ch=0&type=event&ev46[sts=begin'

Edit: I'm not very familiar with Django, but the Request and response object section of their docs states the following:

QueryDict instances are immutable, unless you create a copy() of them. That means you can't change attributes of request.POST and request.GET directly.

Based on my limited reading of those docs, you might be able to apply the unquote() function to the HttpRequest.body attribute and build a new QueryDict out of the results (and possibly use it to update your current one if necessary).

查看更多
登录 后发表回答