I am creating an attachment in Business Central and trying to update attachment content in my application by calling PATCH API "">api.businesscentral.dynamics.com/.../attachmentContent" in my application.
I am reading the attachment file content from blob storage as BinaryData. And, using below code snippet to call PATCH API. The data is updated in attachment in Business Central. However, when we download the attachment file from Business Central, the file is corrupted and unable to open. I am supposed to upload any file formats (like PDF, xlsx, docx, txt, etc).
public HttpResponseMessage UpdatePurchaseInvoiceAttachmentContent(string mediaEditApi, BinaryData binaryData)
{
Console.WriteLine("Method - UpdatePurchaseInvoiceAttachmentContent");
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;
HttpClient httpClient = null;
try
{
httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, mediaEditApi);
httpClient = GetHttpClientWithAuthHeader();
string contentJson = JsonSerializer.Serialize(binaryData.ToString());
httpRequestMessage.Content = new StringContent(contentJson);
httpRequestMessage.Headers.TryAddWithoutValidation("Content-Type","application/json");
httpRequestMessage.Headers.TryAddWithoutValidation("If-Match", "*");
Task<HttpResponseMessage> response = httpClient.SendAsync(httpRequestMessage);
httpResponseMessage = response.Result;
httpResponseMessage.EnsureSuccessStatusCode();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
return httpResponseMessage;
}
Any pointers to update attachment content code snippet will be much appreciated.