web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

How to Send file in body of the PATCH Command using HttpWebRequest in c#

(0) ShareShare
ReportReport
Posted on by

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));

  1. ContentHttpRequest.Method = "PATCH";
  2. httpWebRequest.Headers.Add("Content-Type", "application/octet-stream");
  3. httpWebRequest.Headers.Add("If-Match", "*");
  4. //httpWebRequest.ContentType = "application/octet-stream";
  5. byte[] fileByte = DownloadAttachment(Attachmenturl);
  6. Stream Stream = new MemoryStream(fileByte);
  7. string ResposeJson1 = "";
  8. using (var streamWriter = new StreamWriter(ContentHttpRequest.GetRequestStream()))
  9. {
  10. streamWriter.Write(fileByte);
  11. streamWriter.Close();
  12. }
  13. var httpResponse1 = (HttpWebResponse)ContentHttpRequest.GetResponse();
  14. using (var streamReader = new StreamReader(httpResponse1.GetResponseStream()))
  15. {
  16. ResposeJson1 = streamReader.ReadToEnd();
  17. }
I have the same question (0)
  • Suggested answer
    JAngle Profile Picture
    133 on at

    Did you do a POST first? You need the ID value from that to do the PATCH. The content part looks fine.

  • Community Member Profile Picture
    on at

    yes, I have done POST first and then call PATCH command, I got the ID of the attachment but it gives me an exception of that Bad Request.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans