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

Community site session details

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

Api attachments

(0) ShareShare
ReportReport
Posted on by 35

Hi Team,

Can you please guide me on how to work with the API of "attachment" to attach the document to "Sales Order" or "Sales Invoice"?

I have tried sending the attached request as per documentation, but it fails with the below response:

Request:

AttachmentRequest.txt

Response:

{"error":{"code":"BadRequest","message":"Read called with an open stream or textreader. Please close any open streams or text readers before calling Read.  CorrelationId:  910e6436-db62-48c5-a739-0e87dcdba8be."}} 

Can you please help us in this scenario.

Looking forward to your responses.

Thanks & Regards,

Ankit

I have the same question (0)
  • Suggested answer
    raelhefn Profile Picture
    on at
    RE: Api attachments

    Hi Ankit,

    could you please try to use postman and simply perform the following:

    1. Open postman type post query :

    POST: localhost:6048/.../attachments

    And in Body

    {

       "parentId" : "3af6e05a-075c-eb11-89f9-000d3a2dc4a1", //sales invoice id

       "fileName": "myPDF.pdf"

    }

    make sure requested service is deployed into your environment and it is accessible. 

    community.dynamics.com/.../how-to-test-api-s-in-d365-business-central

  • Ankit Surana Profile Picture
    35 on at
    RE: Api attachments

    Hi Raelhefn,

    Appreciate your response.

    I Have tried this. it works but how to add attachment content in the request? That my main question?

    Can you please also guide me on how to achieve this?

    Looking forward to your response.

    With Kind Regards,

    Ankit

  • raelhefn Profile Picture
    on at
    RE: Api attachments

    It appears that the File Content is a stream that is open during your call. Create Record most likely is complaining about it. Although I am not sure it will work, try saving the File Content to a variable and save the variable to Business Central.

    learning.postman.com/.../

  • Ankit Surana Profile Picture
    35 on at
    RE: Api attachments

    Hi Raelhefn,

    I tried and it still didn't work. This also resulted in the same error response. Can you please help ?

    Request:

    {

     "parentId": "3af6e05a-075c-eb11-89f9-000d3a2dc4a1",

     "fileName": "Work_Order-00005-20210121174035.pdf",

     "byteSize": "44033",

     "attachmentContent": "{{Attachment}}"

    }

    Looking forward to your response.

    With Kind Regards,

    Ankit

  • Verified answer
    alex_li Profile Picture
    60 on at
    RE: Api attachments

    You need first to create an attachment in your BusinessCentral record by sending the POST request as it was described earlier. As a result you receive an attachment id that can be used as follows:

    pastedimage1611846401182v1.png

  • Suggested answer
    JAngle Profile Picture
    127 on at
    RE: Api attachments

    Late to the party but I had to explain this to a customer a few days back so did this post: joshanglesea.wordpress.com/.../

    Since had it working in Power Automate as well.

  • Ankit Surana Profile Picture
    35 on at
    RE: Api attachments

    i tried as per your suggestion , but it still doesnt work.

    Please see the attachment for Reference

    pastedimage1611930087460v1.png

  • Ankit Surana Profile Picture
    35 on at
    RE: Api attachments

    i tried as per your suggestion , but it still doesnt work.

    Please see the attachment for Reference

    pastedimage1611930681852v1.png

  • Suggested answer
    JAngle Profile Picture
    127 on at
    RE: Api attachments

    Change the second v2.0 to v1.0. I got the same error as you until I flipped to that

  • alex_li Profile Picture
    60 on at
    RE: Api attachments

    Hi Josh, is it possible to attach a file to Sales entities through BC api?

    The method we described earlier works perfect for those entities that have Incoming Document tab.

    But for  Sales Order and Sales Invoice  this tab is absent. There is only the "Attachments" one.

    pastedimage1612170950144v1.png

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,915

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,011 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,989 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans