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

Notifications

Announcements

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;

I have the same question (0)
  • Ravi Profile Picture
    22 on at

    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.

  • Suggested answer
    Marco Mels Profile Picture
    on at

    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.

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

News and Announcements

Season of Giving Solutions is Here!

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 1,798

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 812 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 717 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans