| Docs Help
  AppSource  
Docs  /  NVXWUL Web Utility Library  /  Tasks

MultiPart/Form-Data Helper


2024/06/27 • 2 min. to read
For the complex content-type MultiPart/Form-Data there is a helper codeunit for the createn of such contents added.

Allgemein

A multipart/form-data body can be used to send one or more files or fields. This body type can have multiple parameters with name and value. Differnt to a normal json or xml-body you can send here multiple files as a bytestream. Swagger Spezification multipart/form-data

Helper Codeunit

WULMultiPartFormDataHelperNVX

Methoden

InitBody

Prepare the codeunit instance for a new body to create. Sets the boundary to default if it is empty.
procedute InitBody()

AddKey

Add a key with type string to the multipart/form-data body.
procedure AddKey(Name: Text; Value: Text)

AddFile

Add a key with type file to the multipart/form-data body.
procedure AddFile(KeyName: Text; FileName: Text; FileInStream: InStream)

FinishBody

Finish the prepared body.
procedure FinishBody()

SetBoundary

Set a custom boundary string. This must be set before the body is initalized.
procedure SetBoundary(NewBoundary: Text)

GetBoundary

Get the set boundary.
procedure GetBoundary(): Text

GetDefaultBoundary

Get the default boundary string of the codeunit.
procedure GetDefaultBoundary(): Text

GetMultiformData

Get the prepared body after it is finished.
procedure GetMultiformData(var _TempBlob: Codeunit "Temp Blob")

GetMultiPartFormDataContentTypeHTTPHeader

Get the HTTP Header text with the boundary for the HTTP Request.
procedure GetMultiPartFormDataContentTypeHTTPHeader() MultiPartFormDataContentTypeHTTPHeader: Text

Examlpe

The example creates a multipart/form-data body with the a key names "keyvalue1" with the value "myvalue" as well as a second key with name "example_file" with the filename "example.txt" and a file content.
procedure Example(WebAuthorization: Record WULWebAuthorizationNVX; URL: Text[250]; ExampleFileInstream: InStream) ResponseText: Text;
var
    TempBlob: Codeunit "Temp Blob";
    MultiformDataHelper: Codeunit WULMultiPartFormDataHelperNVX;
    WebMessage: Record WULWebMessageNVX;
    ResponseMessage: HttpResponseMessage;
    MultiPartFormDataInStream: InStream;
begin
    Commit();

    MultiformDataHelper.InitBody();
    MultiformDataHelper.AddKey('keyvalue1', 'myvalue');
    MultiformDataHelper.AddFile('example_file', 'example.txt', ExampleFileInstream);
    MultiformDataHelper.FinishBody();

    MultiformDataHelper.GetMultiformData(TempBlob);
    TempBlob.CreateInStream(MultiPartFormDataInStream);
    WebMessage.CreateMessage(Enum::"Http Request Type"::POST, URL, MultiPartFormDataInStream);

    WebMessage.AddWebMessageHeader(Enum::WULWebMessageHeaderTypeNVX::"Request Header", 'Accept', 'application/json');
    WebMessage.AddWebMessageHeader(Enum::WULWebMessageHeaderTypeNVX::"Content Header", 'Content-Type', MultiformDataHelper.GetMultiPartFormDataContentTypeHTTPHeader());
    WebMessage.AddWebMessageAuthorization(WULWebMessageHeaderTypeNVX::"Request Header", WebAuthorization);

    Commit();
    WebMessage.SendMessage(ResponseMessage);
    Commit();
    ResponseText := WebMessage.GetResponse();
end;


Submit feedback for
DE|EN Imprint