I am trying to use URLSearchParams to extract the url parameters whose values are AES encrypted. But I noticed that if the value contains '+' sign, URLSearchParams is not giving proper value.
For example, if url is 'https://www.google.com/?data=a+b', URLSearcParams is giving data as 'a b'. I am using below code.
var url = new URL('https://www.google.com/?data=a+b')
url.searchParams.get('data')
Can anyone please let me know, if there is any other way to extract the url parameter values.
You must use searchParams.append()
in order to set querystring params properly. I made a simple object with all keys + values and then a helper function to append the contents of the objec properly:
const url = new URL('https://www.google.com/')
const params = {
'data': 'a+b',
'moarData': 'c+d'
};
Object.entries(params).forEach(([key, val]) => url.searchParams.append(key, val));
console.log(url.searchParams.get('data'));
console.log(url.searchParams.get('moarData'));