I have angular application where i want to pass plus sign + in query string like:
http://localhost:3000/page?name=xyz+manwal
When I am hitting this URL its converting to:
http://localhost:3000/page?name=xyz%20manwal
Where %20 refer to space . How can I prevent this conversion?
You can override default angular encoding with adding Interceptor which fixes this:
and declare it in providers section of in app.module.ts
I have found solution and posting it for future reference. Angular js was converting
+
sign into%2B
.Following code prevented that:
This is a quite common problem. You can pass it normally in application/x-www-form-urlencoded request. No other request will be able to correctly parse +. They will always parse it into %20 instead of %2B.
You would need to manually manipulate the query parameter, there are 2 ways:
For more info you should reffer to hthe following stack overflow questions Android: howto parse URL String with spaces to URI object? and URL encoding the space character: + or %20?
This ia a common problem. The
+
character is used by the URL to separate two words. In order to use the+
character in the parameter values, you need to encode your parameter values before adding them as part of the URL. Javascript / TypeScript provide aencodeURI()
function for that specific purpose.Here is how you can fix this problem:
In the same way, you can decode the parameters using
decodeURI()
method.In Angular 5.2.7+ the "+" is replaced with space " " in a query string.
Here is the corresponding commit : fix(router): fix URL serialization
If you want to change this behaviour and replace the "+" with "%2B" you can create a custom url serializer and provide it in the AppModule providers.
http://localhost:3000/page?name=xyz+manwal
The URL will be converted to:
http://localhost:3000/page?name=xyz%2Bmanwal
Hope this will help.