Reputation: 1858
I am using Django Rest Framework.
The API receives GET requests with json objects encoded into the URL. For example:
/endpoint?%7B%0D%0A++%22foo%22%3A%5B%22bar%22%2C%22baz%22%5D%0D%0A%7D
Where the decoded parameters are
{
"foo":["bar","baz"]
}
I can't find anything in the documentation for Django or DRF pointing to how the framework can handle this so that I get a QueryDict
with the json objects in it by doing something like:
request.query_params # Should yield a dict -> {foo=[bar,baz]}
How can I decode JSON encoded URLs in Django Rest Framework?
Note that my actual parameters are much more complex. Using POST is not an because the caller relies heavily on caching and bookmarking
Upvotes: 4
Views: 12009
Reputation: 573
def getParams(request):
url_string = request.META["QUERY_STRING"]
params = {}
url_params = url_string.split("&", 1)
for url_param in url_params:
print(url_param)
url_param_values = url_param.split("=", 1)
if len(url_param_values) == 2:
params[url_param_values[0]] = url_param_values[1]
return params
I created this function to get a query parameter that looks like ref=Y3e8rowMetWiBA40f2HtBGsRfb76IOXykAMxcNcog7w=&vef=qDMdvZb+P9sNWbH/dYrDztq79SV6wQ+vf9z1qlUzf3U=
Upvotes: 0
Reputation: 6780
urllib
ought to do it:
from urllib.parse import unquote
url = "endpoint?%7B%0D%0A++%22foo%22%3A%5B%22bar%22%2C%22baz%22%5D%0D%0A%7D"
url = unquote(url)
print(url)
The above almost works, but the encoding might be incorrect, not sure:
endpoint?{
++"foo":["bar","baz"]
}
Upvotes: 1
Reputation: 1122222
The Django request.GET
object, and the request.query_params
alias that Django REST adds, can only parse application/x-www-form-urlencoded
query strings, the type encoded by using a HTML form. This format can only encode key-value pairs. There is no standard for encoding JSON into a query string, partly because URLs have a rather limited amount of space.
If you must use JSON in a query string, it'd be much easier for you if you prefixed the JSON data with a key name, so you can at least have Django handle the URL percent encoding for you.
E.g.
/endpoint?json=%7B%0D%0A++%22foo%22%3A%5B%22bar%22%2C%22baz%22%5D%0D%0A%7D
can be accessed and decoded with:
import json
json_string = request.query_params['json']
data = json.loads(json_string)
If you can't add the json=
prefix, you need to decode the URL percent encoding yourself with urllib.parse.unquote_plus()
, from the request.META['QUERY_STRING']
value:
from urllib.parse import unquote_plus
import json
json_string = unquote_plus(request.META['QUERY_STRING'])
data = json.loads(json_string)
Upvotes: 8