Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

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

Posted on by 20

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;

Categories:
  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels 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
    Ravi 20 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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans