When I try to construct a url
in JavaScript using new URL(url, base)
, it strips the pathname in base
below is my code
const getUrl = () => {
const ids = ["1","2","3"];
const url = new URL("/items", "https://example.com/v1");
url.searchParams.set("id", ids?.toString() ?? "");
return url.toString();
};
console.log(getUrl());
// https://example.com/items?id=1%2C2%2C3
I need the output to be: https://example.com/v1/items?id=1%2C2%2C3
/
or it will be relative to the domain name./
after the base url or the last segment will be replaced by the relative path./items
--> items
.../v1/
const getUrl = () => {
const ids = ["1","2","3"];
const url = new URL("items", "https://example.com/v1/");
url.searchParams.set("id", ids?.toString() ?? "");
return url.toString();
};
console.log(getUrl());