I am making POST requests to an API and I want to make three requests as efficient and with less code as possible. Currently my code works, but it sends only the first object, instead of all three, what is the most efficient way to make three API calls? Here is my attempt so far:
import fetch from 'node-fetch';
import xlsx from 'xlsx';
const baseUrl = "";
const apiToken = "";
const wb = xlsx.readFile('users.xlsx');
const ws = wb.Sheets['users'];
const data = xlsx.utils.sheet_to_json(ws);
let [obj1, obj2, obj3] = data
const optionsForFirstUser = {
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"gtmhub-accountid": accountId,
"Content-type": "application/json; charset=UTF-8",
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify(
obj1
),
};
const optionsForSecondUser = {
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"gtmhub-accountid": accountId,
"Content-type": "application/json; charset=UTF-8",
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify(
obj1
),
};
const optionsForThirdUser = {
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"gtmhub-accountid": accountId,
"Content-type": "application/json; charset=UTF-8",
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify(
obj1
),
};
const createUser = (url, settings) => {
return fetch(`${url}/users`, settings)
.then((response) => response.text())
.then((data) => console.log(data))
.catch((error) => {
console.log(error.message);
});
};
createUser(baseUrl, optionsForFirstUser, optionsForSecondUser, optionsForThirdUser);
I can't use an array, because the API doesn't allow it. It needs to be just JSON objects.