0

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

3
  • Please share your full reproducable code with us so that we can reproduce your issue and get context for the problem. Commented Jan 28, 2019 at 16:59
  • @Jordan Singer my urls.py file is url(r'^api/v1/get_example/$', views.get_example,name='get_example'), and views.py is @api_view(['GET']) def get_example(request): fruits = urllib.unquote(request.GET.get('fruits_name', none)).decode('utf8') print fruits Commented Jan 28, 2019 at 17:11
  • Why do have the spaces quoted (%20) but not the plus sign (%2B) ? In a quoted URL, a plus sign is unquoted to a 'space', that's why you end up with 3 spaces. Commented Jan 28, 2019 at 17:16

3 Answers 3

1

You probably need to use %2B

Ex:

http://exaple.com/api/v1/get_example/?fruits_name=[apple%20%2B%20banana]

Reference

Sign up to request clarification or add additional context in comments.

Comments

0

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]'}

Comments

0

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 '/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.