Reputation: 17
I am reading URL parameter from the below URL
http://exaple.com/api/v1/get_example/?fruits_name=[apple%20+%20banana]
fruits = urllib.unquote(request.GET.get('fruits_name', None)).decode('utf8')
print fruits
my output is: [apple banana]
in between apple and banana I am getting three spaces but not +
symbol in my output. the original string is [apple + banana]
. I need output as [apple + banana]
.
Can anyone suggest where I am doing wrong??
Upvotes: 0
Views: 804
Reputation: 29
Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL.The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'
Upvotes: 0
Reputation: 18136
You could split query string on your own to preserve the plus sign:
from urllib.parse import urlparse, unquote
u = 'http://exaple.com/api/v1/get_example/?fruits_name=[apple%20+%20banana]'
o = urlparse(u)
qs = unquote(o.query)
queryDict = {k: v for (k, v) in [x.split("=", 1) for x in qs.split("&")]}
print(queryDict)
Prints:
{'fruits_name': '[apple + banana]'}
Upvotes: 0