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

View File

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