I want to attach a file to a Purchase Invoice in the Incoming Attachment Tab in dynamic 365 Business central.
to achieve this i am using PATCH Command https://{businesscentralPrefix}/api/v1.0/companies({companyId})/attachments(parentId={parentId},id={attachmentId})/content
I need to send the file in the body using HttpWebRequest.
I have tried to send the stream of the file but every time gives me a 400 bad request exception.
Please refer my below code and guide me to attach a file .
#region SendContentToattachment
var details = JObject.Parse(ResposeJson);
string ContentUrl =Convert.ToString(details["content@odata.mediaEditLink"]);
var ContentHttpRequest = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(ContentUrl));
ContentHttpRequest.Method = "PATCH";
httpWebRequest.Headers.Add("Content-Type", "application/octet-stream");
httpWebRequest.Headers.Add("If-Match", "*");
//httpWebRequest.ContentType = "application/octet-stream";
byte[] fileByte = DownloadAttachment(Attachmenturl);
Stream Stream = new MemoryStream(fileByte);
string ResposeJson1 = "";
using (var streamWriter = new StreamWriter(ContentHttpRequest.GetRequestStream()))
{
streamWriter.Write(fileByte);
streamWriter.Close();
}
var httpResponse1 = (HttpWebResponse)ContentHttpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse1.GetResponseStream()))
{
ResposeJson1 = streamReader.ReadToEnd();
}