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

Unable to upload file Instream to a Custom API using Multipart/Form-Data

(1) ShareShare
ReportReport
Posted on by 22

Hi,
i am consuming an API, where i need to upload on document(instream).
for uploading, i am running Upload mutation of GraphQL so using multipart/form-data.
But getting few errors:
Error 1: Multipart field missing 'Operation'
Error 2: Misordered multipart fields; files should follow 'map'.

Sample Code :

procedure UploadFile(UploadStream: InStream)
var
TempBlob: Codeunit "Temp Blob";
PayloadOutStream: OutStream;
PayloadInStream: InStream;
CR: Char;
LF: Char;
NewLine: Text;
Content: HttpContent;
ContentHeaders: HttpHeaders;
Client: HttpClient;
Request: HttpRequestMessage;
Response: HttpResponseMessage;
OutputResultJson: Text;
begin
CR := 13;
LF := 10;
NewLine += '' + CR + LF;
Content.GetHeaders(ContentHeaders);
ContentHeaders.Clear();
ContentHeaders.Add('Content-Type', 'multipart/form-data;boundary=boundary');

TempBlob.CreateOutStream(PayloadOutStream);
PayloadOutStream.WriteText('--boundary' + NewLine);
// key1 value1
PayloadOutStream.WriteText('Content-Disposition: form-data; name="operations"' + NewLine);
PayloadOutStream.WriteText(NewLine);
PayloadOutStream.WriteText(' {"query": "mutation($file: Upload!){\n upload(input:{ file: $file}){\n uniqueId\n message\n code\n }\n}"}' + NewLine);
PayloadOutStream.WriteText('--boundary' + NewLine);
// key2 value2
PayloadOutStream.WriteText('Content-Disposition: form-data; name="map"' + NewLine);
PayloadOutStream.WriteText(NewLine);
PayloadOutStream.WriteText('{"0":["variables.file"]}' + NewLine);
PayloadOutStream.WriteText('Content-Type: application/octet-stream' + NewLine);
PayloadOutStream.WriteText(NewLine);
// Copy all bytes from the uploaded file to the stream.
CopyStream(PayloadOutStream, UploadStream);
PayloadOutStream.WriteText(NewLine);
PayloadOutStream.WriteText('--boundary');
// Copy all bytes from the write stream to a read stream.
Tem PayloadOutStream.WriteText('--boundary' + NewLine);
// file1
PayloadOutStream.WriteText('Content-Disposition: form-data; name="0"; fileName="Test.pdf"' + NewLine);
PpBlob.CreateInStream(PayloadInStream);
PayloadInStream.ReadText(OutputResultJson);
// Write all bytes from the request body stream.
Content.WriteFrom(PayloadInStream);
Request.Content := Content;
Request.SetRequestUri('https://SampleAPIUrl.com/');
Request.Method := 'POST';
Client.Send(Request, Response);
if not Response.IsSuccessStatusCode() then
Message(Response.ReasonPhrase + '\Error Code : ' + format(Response.HttpStatusCode)); //RV

Response.Content().ReadAs(OutputResultJson);
Message(OutputResultJson);
end;

  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: Unable to upload file Instream to a Custom API using Multipart/Form-Data

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Ravi Profile Picture
    22 on at
    RE: Unable to upload file Instream to a Custom API using Multipart/Form-Data

    I found one article from this community :

    community.dynamics.com/.../how-to-send-pdf-file-to-multipart-form-data-web-services-in-dynamics-nav

    But there is some issue with it. Images are not loading and related link is not opening as well.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,336 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans