{"id":190,"date":"2022-05-03T14:48:47","date_gmt":"2022-05-03T14:48:47","guid":{"rendered":"https:\/\/privateblog.nobytesleft.net\/?p=190"},"modified":"2022-05-03T14:48:47","modified_gmt":"2022-05-03T14:48:47","slug":"encoding-url-query-parameters-in-javascript-django","status":"publish","type":"post","link":"https:\/\/privateblog.nobytesleft.net\/?p=190","title":{"rendered":"Encoding URL query parameters in Javascript + Django"},"content":{"rendered":"<p>When passing arbitrary strings as parameters for some function, you may be tempted to do it using a path component, which can be decoded easily with Django.<\/p>\n<p>Do not do it. Forward slashes will be encoded in arbitrary ways (and you should note that the &#8220;string&#8221; path component in Django excludes forward slashes). Use query parameters instead, as follows.<\/p>\n<p>In Javascript:<\/p>\n<pre class=\"lang:js decode:true\">searchParams = new URLSearchParams({'parameter': 'value'});\r\nURLparams = searchParams.toString();\r\n\/\/ use this string to build your URL\r\nURL = \"http:\/\/&lt;server&gt;\/endpoint\" + URLparams;\r\n<\/pre>\n<p>In Django:<\/p>\n<pre class=\"lang:python decode:true\"># url\r\n    path('endpoint', views.endpoints.endpoint_function),\r\n# view function in endpoint\r\ndef endpoint_function(request):\r\n    parameter = request.query_params.get('parameter', DEFAULT_VALUE)\r\n<\/pre>\n<p>This will ensure correct encoding and decoding of parameters.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When passing arbitrary strings as parameters for some function, you may be tempted to do it using a path component, which can be decoded easily with Django. Do not do&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=190"}],"version-history":[{"count":2,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/190\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}