
I use d365 batch web api with javascript . but meet below error
var data=[]
data.push('--batch_123456')
data.push('Content-Type: multipart/mixed;boundary=changeset_createRequest')
data.push('--changeset_createRequest')
data.push('Content-Type: application/http')
data.push('Content-Transfer-Encoding:binary')
data.push('Content-ID:1')
data.push('POST https://org1b1f8e1a.crm5.dynamics.com/api/data/v9.2/aia_s2c_sourcingrequests HTTP/1.1')
data.push('Content-Type: application/json;type=entry')
data.push('{"aia_requeststatus":914710002,"transactioncurrencyid@odata.bind":"/transactioncurrencies(576fe49b-1d68-ec11-8941-0022485aa251)","aia_initialcostestimation":12313,"aia_initialcostestimationinusd":12313,"aia_AIALegalEntity@odata.bind":"/aia_aialegalentities(3ad86a12-00f2-ec11-bb3d-6045bd1bfea1)","aia_targetstartdate":"2022-12-05T16:00:00.000Z","aia_contractduration":1,"aia_contractdurationunit":914710001,"aia_ContractOwner@odata.bind":"/systemusers(c1d0eeb3-5477-ec11-8d21-0022485a8ab3)","aia_requesttitle":"1231312","aia_briefdescription":"123123","aia_projectname":"123","aia_category":914710004,"aia_Requestor@odata.bind":"/systemusers(c1d0eeb3-5477-ec11-8d21-0022485a8ab3)","aia_supplier_question_1":914710000,"ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)","aia_aia_s2csupplier_SourcingRequest_aia_s2c_s":[{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"}]}')
data.push('--batch_123456--')
var payload = data.join('\n\r')
$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() '/api/data/v9.2/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'
},
data: payload,
async: false,
success: function (data, textStatus, xhr) {
console.log("Record has been successfully Created");
},
error: function (xhr, textStatus, errorThrown) {
console.log(textStatus " " errorThrown);
}
});
excute in the chrome request playload
--batch_1232322
Content-Type: multipart/mixed;boundary=changeset_createRequest
--changeset_createRequest
Content-Type:application/http
Content-Transfer-Encoding:binary
Content-ID:createRequest
POST https://org1b1f8e1a.crm5.dynamics.com/api/data/v9.2/aia_s2c_sourcingrequests HTTP/1.1
Content-Type:application/json;type=entry
{"aia_requeststatus":914710002,"transactioncurrencyid@odata.bind":"/transactioncurrencies(576fe49b-1d68-ec11-8941-0022485aa251)","aia_initialcostestimationinusd":0,"aia_AIALegalEntity@odata.bind":"/aia_aialegalentities(3ad86a12-00f2-ec11-bb3d-6045bd1bfea1)","aia_Requestor@odata.bind":"/systemusers(c1d0eeb3-5477-ec11-8d21-0022485a8ab3)","aia_supplier_question_1":914710000,"ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)","aia_aia_s2csupplier_SourcingRequest_aia_s2c_s":[{"aia_Supplier@odata.bind":"/accounts(7be764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(7be764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(7be764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"}]}
--changeset_createRequest--
--batch_1232322--
the response error live below
{
"Message": "The 'Content-Type' header is missing. The 'Content-Type' header must be specified for each MIME part of a batch message.",
"ExceptionMessage": "The 'Content-Type' header is missing. The 'Content-Type' header must be specified for each MIME part of a batch message.",
"ExceptionType": "Microsoft.OData.ODataException",
"StackTrace": " at Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchReaderStream.ValidatePartHeaders(ODataBatchOperationHeaders headers, Boolean& isChangeSetPart)\r\n at Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchReaderStream.ProcessPartHeader(String& contentId)\r\n at Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchReader.SkipToNextPartAndReadHeaders()\r\n at Microsoft.OData.ODataBatchReader.ReadImplementation()\r\n at Microsoft.OData.ODataBatchReader.InterceptException[T](Func`1 action)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataBatchHandler.d__12.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataBatchHandler.<>c__DisplayClass9_0.<b__0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.d__11`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.d__15`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.d__15`2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataBatchHandler.d__9.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.OData.Batch.DefaultODataBatchHandler.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Batch.HttpBatchHandler.d__0.MoveNext()",
"ErrorCode": "0x0"
} I have fig out the problem by correct with below code. There is a litter change.
var data=[]
data.push('--batch_123456')
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456')
data.push('');
data.push('--changeset_BBB456')
data.push('Content-Type: application/http')
data.push('Content-Transfer-Encoding:binary')
data.push('Content-ID:1')
data.push('');
data.push('POST org1b1f8e1a.crm5.dynamics.com/.../aia_s2c_sourcingrequests HTTP/1.1')
data.push('Content-Type: application/json;type=entry')
data.push('');
data.push('{"aia_requeststatus":914710002,"transactioncurrencyid@odata.bind":"/transactioncurrencies(576fe49b-1d68-ec11-8941-0022485aa251)","aia_initialcostestimation":12313,"aia_initialcostestimationinusd":12313,"aia_AIALegalEntity@odata.bind":"/aia_aialegalentities(3ad86a12-00f2-ec11-bb3d-6045bd1bfea1)","aia_targetstartdate":"2022-12-05T16:00:00.000Z","aia_contractduration":1,"aia_contractdurationunit":914710001,"aia_ContractOwner@odata.bind":"/systemusers(c1d0eeb3-5477-ec11-8d21-0022485a8ab3)","aia_requesttitle":"1231312","aia_briefdescription":"123123","aia_projectname":"123","aia_category":914710004,"aia_Requestor@odata.bind":"/systemusers(c1d0eeb3-5477-ec11-8d21-0022485a8ab3)","aia_supplier_question_1":914710000,"ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)","aia_aia_s2csupplier_SourcingRequest_aia_s2c_s":[{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"},{"aia_Supplier@odata.bind":"/accounts(81e764c6-caf2-ec11-bb3e-000d3a08c737)","ownerid@odata.bind":"/teams(d2600bcb-fef1-ec11-bb3d-6045bd1bfea1)"}]}')
data.push('--changeset_BBB456--');
data.push('--batch_123456--');
var payload = data.join('\r\n');
$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'
},
data: payload,
async: false,
success: function (data, textStatus, xhr) {
alert("Record has been successfully Created");
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});