use URL api to construct API url

avoids imperfect logic detecting `?`
This commit is contained in:
Min RK
2024-03-08 09:05:55 +01:00
parent 78a796cea6
commit 8cbe1eac2b
2 changed files with 4 additions and 7 deletions

View File

@@ -3,14 +3,11 @@ const base_url = jhdata.base_url || "/";
const xsrfToken = jhdata.xsrf_token;
export const jhapiRequest = (endpoint, method, data) => {
let api_url = `${base_url}hub/api`;
let suffix = "";
let api_url = new URL(`${base_url}hub/api` + endpoint, location.origin);
if (xsrfToken) {
// add xsrf token to url parameter
var sep = endpoint.indexOf("?") === -1 ? "?" : "&";
suffix = sep + "_xsrf=" + xsrfToken;
api_url.searchParams.set("_xsrf", xsrfToken);
}
return fetch(api_url + endpoint + suffix, {
return fetch(api_url, {
method: method,
json: true,
headers: {

View File

@@ -4,7 +4,7 @@ import { jhapiRequest } from "./jhapiUtil";
const withAPI = withProps(() => ({
updateUsers: (options) => {
let params = new URLSearchParams();
params["include_stopped_servers"] = "1";
params.set("include_stopped_servers", "1");
for (let key in options) {
params.set(key, options[key]);
}