user3202086
user3202086

Reputation: 17

Python - special characters reading from URL parameter

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

Answers (3)

sampath kumara
sampath kumara

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

Maurice Meyer
Maurice Meyer

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

Rakesh
Rakesh

Reputation: 82795

You probably need to use %2B

Ex:

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

Reference

Upvotes: 1

Related Questions