user3687899
user3687899

Reputation: 81

Delete data from D365 in Batch Request, getting the error 'Content-Type' Header is missing

I am trying to create an Azure function to delete some data from a Dynamics 365 CE instance. The plan is to use the D365 WebAPI and the Batch Operations request to establish this.

Currently encountering an issue while sending a request after creating the batch request.

I have been referring to this documentation from Microsoft: https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/execute-batch-operations-using-web-api


The code looks like:

var batchId = Guid.NewGuid().ToString();
                        log.LogInformation($"Batch Request Id = {batchId}.");
                        HttpRequestMessage deleteBatchRequestMessage = new HttpRequestMessage(HttpMethod.Post, "$batch");
                        deleteBatchRequestMessage.Content = new MultipartContent("mixed", "batch_" + batchId);

HttpClient client = new HttpClient();
                        client.BaseAddress = new Uri(d365Url);
                        // Default Request Headers needed to be added in the HttpClient Object
                        client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                        client.DefaultRequestHeaders.Add("OData-Version", "4.0");
                        client.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
                        d365HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        // Set the Authorization header with the Access Token received specifying the Credentials
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", d365Token);

                        HttpResponseMessage response = await client.SendAsync(deleteBatchRequestMessage);
                        var ass = await response.Content.ReadAsStringAsync();

But I keep getting the exception: 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."

Upvotes: 0

Views: 473

Answers (1)

Martin
Martin

Reputation: 39

Is there any reason why you use WebApi? You can use the SDK and the IOrganizationService handle Everything. This will make your life very easy

http://www.threadpunter.com/azure/using-azure-functions-to-call-dynamics-365/

Upvotes: 1

Related Questions