JavaScript URL Decode function
Here is a complete function (taken from PHPJS): function urldecode(str) { return decodeURIComponent((str+”).replace(/\+/g, ‘%20’)); }
Here is a complete function (taken from PHPJS): function urldecode(str) { return decodeURIComponent((str+”).replace(/\+/g, ‘%20’)); }
str = “\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a”.force_encoding(‘ASCII-8BIT’) puts CGI.escape str => “%124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A”
For ASP.NET Core 2.0+ just add System.Net namespace – WebUtility class is shipped as part of System.Runtime.Extensions nuget package, that is referenced by default in ASP.NET Core project. For the previous version add Microsoft.AspNetCore.WebUtilities nuget package. Then the WebUtility class will be available for you: public static class WebUtility { public static string UrlDecode(string encodedValue); … Read more
Here you go: function encodeQueryData(data) { const ret = []; for (let d in data) ret.push(encodeURIComponent(d) + ‘=’ + encodeURIComponent(data[d])); return ret.join(‘&’); } Usage: const data = { ‘first name’: ‘George’, ‘last name’: ‘Jetson’, ‘age’: 110 }; const querystring = encodeQueryData(data);
I had significant headaches with these methods before, I recommend you avoid any variant of UrlEncode, and instead use Uri.EscapeDataString – at least that one has a comprehensible behavior. Let’s see… HttpUtility.UrlEncode(” “) == “+” //breaks ASP.NET when used in paths, non- //standard, undocumented. Uri.EscapeUriString(“a?b=e”) == “a?b=e” // makes sense, but rarely what you // … Read more
This behaves as expected. The URLEncoder implements the HTML Specifications for how to encode URLs in HTML forms. From the javadocs: This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. and from the HTML Specification: application/x-www-form-urlencoded Forms submitted with this content type must be encoded as follows: Control names and … Read more
I didn’t find the existing answers satisfactory so I decided to dig a little deeper to settle this issue. Surprisingly, the answer is very simple: There is (almost) no valid reason to ever use Uri.EscapeUriString. If you need to percent-encode a string, always use Uri.EscapeDataString.* * See the last paragraph for a valid use case. … Read more
Without seeing your code, it’s hard to answer other than a stab in the dark. I would guess that the string you’re passing to encodeURIComponent(), which is the correct method to use, is coming from the result of accessing the innerHTML property. The solution is to get the innerText/textContent property value instead: var str, el … Read more
System.Uri.EscapeUriString() can be problematic with certain characters, for me it was a number / pound ‘#’ sign in the string. If that is an issue for you, try: System.Uri.EscapeDataString() //Works excellent with individual values Here is a SO question answer that explains the difference: What’s the difference between EscapeUriString and EscapeDataString? and recommends to use … Read more
You don’t encode the entire URL, only parts of it that come from “unreliable sources”. Java: String query = URLEncoder.encode(“apples oranges”, “utf-8”); String url = “http://stackoverflow.com/search?q=” + query; Kotlin: val query: String = URLEncoder.encode(“apples oranges”, “utf-8”) val url = “http://stackoverflow.com/search?q=$query” Alternatively, you can use Strings.urlEncode(String str) of DroidParts that doesn’t throw checked exceptions. Or use … Read more