AngularJS truncates the trailing equal signs when routing to them. I have a base64 string which need to be added as a query parameter to the url(route)
ex: http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg==
this loads as:
http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg
Is there a workaround for this?
If you try to set route as a string, then you need to escape = sign. That's because this character has a special meaning in the query string - it separates parameter name from its value.
So, one solution could be:
var query = "code=NnuW3q49QW38Mf-Cg==";
$location.url("/some/path?" + encodeURIComponent(query));
What encodeURIComponent() will do, is it will replace all special characters - = will be replaced with %3D for instance. This will prevent it from being interpreted as a key-value separator.
If you only want to change the query string parameters, not the whole URL, you can also use $location.search() method:
$location.search("code", "NnuW3q49QW38Mf-Cg==");
Just remember to pass two parameters to that method, not one. If you do:
$location.search("code=NnuW3q49QW38Mf-Cg==");
the = sign will not get escaped, only stripped.
Angular uses parseKeyValue() function internally to parse query string, it can be found here. You can see that the split is being done over the = sign. That's why they get stripped.
But if you take a look at .search() method implementation, you see that parseKeyValue() is being called only if you supply one argument to .search(). It's not invoked if you supply name of the parameter as a first, and value as a second argument.
Peeking at the source code also suggests yet another solution:
$location.search({"code": "NnuW3q49QW38Mf-Cg=="});
Related
I am writing a wrapper around react-router's useSearchParams() hook that automatically handles the (de)serialization of individual search parameters. In my unit test I am attempting to verify that calling the update function returned from the useSearchParams() function will also update the location's search property properly (handling any encoding automatically).
My unit test is nearly working, but the final check is failing. I am expecting the updated value to be $%{ }#=! - and when I use encodeURIComponent on it it becomes: "%24%25%7B %20 %7D%23%3D !"
However, the value that I get back for the parameter from useLocation()'s search property is "%24%25%7B + %7D%23%3D %21"
(white space added so I can bold things for emphasis)
While very similar, and clearly the same value when decoded, react-router is encoding certain characters ever so slightly differently from the standard encodeURIComponent function. Here we see that the whitespace character is getting encoded as %20 by encodeURIComponent and as + by react-router. The exclamation mark is being left simply as ! by encodeURIComponent while react-router has encoded it as %21
Is there a standard function somewhere that will allow me to use and test against react-router's encoding? Or is there any documentation describing the differences between react-router's encoding vs the standard encodeURIComponent function so that I might write my own?
Thanks!
I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?
I am looking to have a regex for passing a value in the array to comma separated. Here is the regex I used for the fetching the value.
Regex: id="selectedAgency(.+?)" currently using this regex I am able to find the below value which is matching in
id="selectedAgency[1]"
Hence the outcome I receives is as follows:
&selectedAgencies=[14],[12],[10],[9]
However I would like to have the actual output as the below:
&selectedAgencies= 14,12,10,9
To remove [] use different regex:
Regex : id="selectedAgency\[(\d+)
not sure if this JavaScript version works for you but here it is. Hopefully the regex and replace part should help.
let str = "&selectedAgencies=[14],[12],[10],[9]";
let result = str.replace(/\[(\d+)\]/g, "$1"); //&selectedAgencies=14,12,10,9
I have a route that consumes a file and splits it:
from("file:etc.")
.split(body().tokenize("\n", 100, false)).streaming()
The second parameter is set to a constant 100. Is there a way to have it wary, based on the file. For instance, just before I hit the "split" I could set a value in the header, and it would be cool if I could then do:
.split(body().tokenize("\n", simple("...pull header value..."), false))
but, tokenize() needs an integer, not an expression.
Is there a neat way to achieve what I want: varying the value of 'group'?
I haven't seen that you can pass expressions to your tokenize method. However you can do like this:
.split().method("mySplitterBean", "splitBody")
Pass the entire exchange and then you can access the headers and properties.
See an example here under streaming mode pojo;
http://camel.apache.org/splitter.html
I was wondering how to make a GET call passing a param that contains the '/' character. Is it possible?
Example: GET /service/products/GIANAX W/22 cps
Thank you all very much!
Use encodeURIComponent on the piece of the URL that contains the / and use the resulting string as your actual URL.
For example, if you call encodeURIComponent() on "GIANAX W/22 cps" and combine it back with the other part of the URL, you will get:
/service/products/GIANAX%20W%2F22%20cps
Note, you can only call encodeURIComponent on one path segment at a time. You can't call it on the whole URL or otherwise it will escape the actual path separators that you want to retain.