Garnosny
Garnosny

Reputation: 1

How to relevant data get from PHP api

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

The only difference is inside between "smith" and "jordan"

Not sure how this was generated.

Upvotes: 0

Views: 47

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

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

Related Questions