I am trying to insert multiple records using JavaScript web resource in PowerApps model driven application. Currently I am able to insert one record into the table using below HTTP request.
var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/[entity name]", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200 || req.status === 204) {
console.log(JSON.parse(req.responseText));
} else {
console.log("Error code: " + req.status);
}
}
};
const body = JSON.stringify({
prop1: "12345",
prop2: "test data",
});
req.send(body);
Now I am trying to insert multiple records in single HTTP request, but unable to do so. Below is the code which I have tried implementing
var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/[entity name]", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200 || req.status === 204) {
console.log(JSON.parse(req.responseText));
} else {
console.log("Error code: " + req.status);
}
}
};
const body = JSON.stringify([
{
prop1: "12345",
prop2: "test data",
},
{
prop1: "12345",
prop2: "test data",
},
]);
req.send(body);
But getting below error
{"error":{"code":"0x80048d19","message":"Error identified in Payload provided by the user for Entity :'[Entity Name]', For more information on this error please follow this help link https://go.microsoft.com/fwlink/?linkid=2195293 ----> InnerException : Microsoft.OData.ODataException: Invalid JSON. More than one value was found at the root of the JSON content. JSON content can only have one value at the root level, which is an array, an object or a primitive value.
I tried below body format too, but still not able to identify the solution
const body = JSON.stringify(
{
prop1: "12345",
prop2: "test data",
},
{
prop1: "12345",
prop2: "test data",
}
);
Kindly let me know if there is any way in JavaScript to insert multiple records in a single request.
Thanks in advance
EDIT 1:
As per suggestion from Guido Prieite I tried executing the batch request. Below is the code which I have tried.
var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/$batch", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", 'multipart/mixed; boundary="batch_80dd1615-2a10-428a-bb6f-0e559792721f"');
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200 || req.status === 204) {
console.log(JSON.parse(req.responseText));
} else {
console.log("Error code: " + req.status);
}
}
};
const body = '--batch_80dd1615-2a10-428a-bb6f-0e559792721f\nContent-Type: application/http\nContent-Transfer-Encoding: binary\n\nPOST /api/data/v9.2/[Entity Name] HTTP/1.1\nContent-Type: application/json; type=entry\n\n{\n"prop1": "1234567",\n"prop2": "test1"\n}\n--batch_80dd1615-2a10-428a-bb6f-0e559792721f\nContent-Type: application/http\nContent-Transfer-Encoding: binary\nPOST /api/data/v9.2/[Entity Name] HTTP/1.1\nContent-Type: application/json; type=entry\n\n{\n"prop1": "123456789",\n"prop2": "test new new"\n}\n--batch_80dd1615-2a10-428a-bb6f-0e559792721f';
req.send(body);
But I am receiving below error
ExceptionMessage":"The message header 'POST /api/data/v9.2/[Entity Name] HTTP/1.1' is invalid. The header value must be of the format 'header name: header value'.","ExceptionType":"Microsoft.OData.ODataException"
Any help on this