-1

Whenever I try to to get my query string parameter everything works but only + sign gets stripped.

Here is url file:

urlpatterns = [
re_path(r'^forecast/(?P<city>[\w|\W]+)/$', weather_service_api_views.getCurrentWeather)]

Here is view File:

@api_view(['GET'])
def getCurrentWeather(request, city):
   at = request.GET["at"]
   print(at)

   return JsonResponse({"status": "ok"}, status=200)

So if I hit the server with this URL:

http://192.168.0.5:8282/forecast/Bangladesh/?at=2018-10-14T14:34:40+0100

the output of at is like this:

2018-10-14T14:34:40 0100

Always + sign gets stripped. No other characters get stripped. I have used characters like !, = , - etc.

Yunus
  • 405
  • 5
  • 18
  • Does this answer your question? [Plus sign in query string](https://stackoverflow.com/questions/6855624/plus-sign-in-query-string) Also: [Does a `+` in a URL scheme/host/path represent a space?](https://stackoverflow.com/questions/1005676/does-a-in-a-url-scheme-host-path-represent-a-space). – Abdul Aziz Barkat Sep 06 '21 at 13:40
  • Yes but partially. How to resolve the issue in Django? – Yunus Sep 06 '21 at 13:44
  • As answers to those questions state you need to urlencode the `+` to be `%2B`. – Abdul Aziz Barkat Sep 06 '21 at 13:53
  • Thanks a lot. I have done another solution based on the solution you provided. URL encoding for me right now will be a heavy task. – Yunus Sep 06 '21 at 13:55
  • urlencoding is not a very difficult task... for curl: [How to urlencode data for curl command?](https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command). Please research a bit before asking any question... – Abdul Aziz Barkat Sep 06 '21 at 14:17

1 Answers1

1

Since + is a special character, you will have to encode your value. Where to encode? it depends how are you generating the values for at. Based on your URL's and endpoints it looks like you are working on a weather app and at value is generated by Javascript. You can encode your values with encodeURIComponent

let at = encodeURIComponent(<your_existing_logic>)

eg:

let at = encodeURIComponent('2018-10-14T14:34:40+0100')

it will return a result

'2018-10-14T14%3A34%3A40%2B0100'

then in your backend you can get that value with: at = request.GET.get('at') it will give you the desired value, 2018-10-14T14:34:40+0100 in this case.

If you are creating your at param in your backend, then there are multiple ways to achieve that. You can look into this solution: How to percent-encode URL parameters in Python?

Nalin Dobhal
  • 2,292
  • 2
  • 10
  • 20
  • Thanks for explaining. However I am using curl and this data can be given on terminal. So I assume there is no way to encode URL param in curl. – Yunus Sep 06 '21 at 14:08
  • check this one: https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command – Nalin Dobhal Sep 06 '21 at 15:02