Decode Code Parameters

I am talking to a server that used HTTP strings to send me as follows:

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

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

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

This breaks up our parser, which expects a line similar to the first.

Is there any way to β€œde-encode” a string so that I can use the old code (so that it is not broken when re-writing the parser)?

According to the answer below, we can use urllib.unquote () to clear the line up. However, we rely on request.GET, which is customizable based on the first line. Is it possible to restore a GET object based on a new converted string or somehow force it to be reevaluated?

+4
source share
1 answer

I suspect you need 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 am not very familiar with Django, but the Request and Response Object section of their documents contains the following:

QueryDict instances are immutable unless you create them (). This means that you cannot directly modify the request.POST and request.GET attributes.

Based on my limited reading of these documents, you can apply the unquote() function to the HttpRequest.body attribute and build a new QueryDict from the results (and perhaps use it to update the current if necessary).

+5
source

All Articles