0

I had used unquote in my urls before to get special characters in the url like so:

path('example/<str:phone_number>', views.find_user_by_phone_number.as_view(), name='api.find_user_by_phone_number'),
#call with example/+849323212231

and in my views:

from urllib.parse import unquote

.....

   phone_number = unquote(phone_number)
   print(phone_number)

It worked great and i got +849323212231 with special character +

But now i need to make url with multiple parameters that can has phone_number in it:

path('example', views.find_user_by_phone_number.as_view(), name='api.find_user_by_phone_number'),
    #call with example?phone_number=+849323212231

With view to handle parameter:

...
  phone_number = unquote(request.GET.get('phone_number'))
  print(phone_number)

And the result from the print i got was " 849323212231" with whitespace at the start.

Seems like value from request.GET.get('phone_number') return " 849323212231" so plus turned into whitespace.

Expected to get +849323212231 from request.GET.get('phone_number')

How do i get special character with named parameter in url ?

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • have you tried `urlunquote_plus` – Exprator Jan 13 '20 at 06:53
  • @Exprator yes i had tried with `phone_number = unquote_plus(request.GET.get('phone_number'))` it still return " 849323212231" without plus – Linh Nguyen Jan 13 '20 at 06:56
  • hmm the problem is, u r escaping it while taking the phone number only, use the whole url like `unqoute(url)` then get the `phone_number` – Exprator Jan 13 '20 at 07:00
  • `request.get_full_path()` return `/example?phone_number= 849323212231 ` so i guess i can't even get it from the full url path – Linh Nguyen Jan 13 '20 at 07:19
  • plus sign is a reserved character, can you show how you are submitting those values? form or ajax? add the code here. – Nalin Dobhal Jan 13 '20 at 08:56
  • i don't use the form, i just call the GET api from postman. As the code my get request in django only has that for now, the request.GET worked for other parameters , just having difficulty with special characters in parameter value – Linh Nguyen Jan 13 '20 at 08:59

0 Answers0