I would like to know how I could insert a json with multiple elements.
It seems that from the documentation I can only insert 1 article at a time:
fetch('https://cockpit.tld/api/content/item/posts', {
method: 'POST',
headers: {
"api-key": "a2ea86ea065a6d2301a8b4a535bc",
"Content-Type": "application/json"
},
body: JSON.stringify({
data: { item1, item2, itemN }
})
})
.then(response => response.json())
.then(response => console.log(response));
To insert more elements I had to do a loop.
Example:
bookings.forEach(async (booking) => {
try {
const response = await fetch(
`${process.env.HOST}/api/content/item/bookings`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data: booking }),
}
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const result = await response.json();
console.log("booking :", result);
} catch (error) {
console.error("Error:", error);
}
});