Reputation: 1
I need to append params to my query, but I always get different results. I tried a few ways but without success. Check code below.
First check my code which work without new URL (I need to use new URL
).
let url = `${getBaseUrl()}/myUrl/search?surnames=${allData.join('%0A')}`
This works well, but when I use new URL
:
let url = new URL('myUrl/search' , getBaseUrl())
url.searchParams.append('surnames' , allData);
The code above doesn't work, and I don't know the reason why?
I tried to inspect url and see different
search?surnames=smith%0Ajordan
search?surnames=smith%250Ajordan
The only difference is inside between "smith" and "jordan"
%0A
work%250A
doesn't workNot sure how this was generated.
Upvotes: 0
Views: 47
Reputation: 48693
The original %
is being encoded as %25
, so %0
becomes %250
.
Try using an unencoded line feed (\n
):
const
allData = ['smith', 'jordan'],
getBaseUrl = () => 'https://localhost:8080',
url = new URL('myUrl/search' , getBaseUrl());
url.searchParams.append('surnames', allData.join('\n'));
console.log(url); // https://localhost:8080/myUrl/search?surnames=smith%0Ajordan
Upvotes: 1