allow makeAPIRequest() to handle JSON requests

This commit is contained in:
Thomas Rubini 2023-03-12 12:56:04 +01:00
parent e1ea41b6d1
commit 25eeae34ba
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373

View File

@ -6,12 +6,22 @@
* @returns a Promise, which resolves when the server can be reached and responds without an error
* and rejects otherwise
*/
async function makeAPIRequest(endpoint, body) {
async function makeAPIRequest(endpoint, body, options={}) {
let fetchOptions = {
method: "POST",
headers: {
'Accept': 'application/json'
}
};
if (options["content"] === 'json') {
fetchOptions["headers"]["Content-Type"] = 'application/json'
fetchOptions["body"] = JSON.stringify(body)
} else {
fetchOptions["body"] = new URLSearchParams(body);
}
return new Promise((resolve, reject) => {
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
};
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
const responseCode = response.status;