Send multiple files as multipart/form data using web request

Hi!

I would like to send POST requests to another system with attachments. 3rd party system expects that payload will be multipart/form data with an array of files in the body.
Number of files to send can vary, for one case it will be 1 file, for another it will be 3 files.

Question is - is it possible to send multiple files in one request? I.e. i have a file object class with cardinality many, is it possible to send all objects from it in one request?

Hi,

For clients, the File Object Class has a property called fileData that might be useful for you. fileData contains the binary data of the file in a Blob which you can use to POST to your 3rd party system. You can access this field in the function editor with fileObjectClassName.__file (2 underscores). Also note that you will have to slice the Blob from 0 to file.length to have Appfarm return it as an blob.

You can then use JS code in a web requests body to iterate over all objects in your File Object Class (Many) and add them to an array. Then you can send the whole array as an JSON object to your 3rd party system.

There are two prerequistes however:

  1. Files over 1GB might hang or crash your web browser and are not recommended.

  2. The web request must be sent from a client. Blobs are stored in your browser session, not on Appfarms servers, so you must check the send from client box on your web request. This also means that you will not be able to send the data from a service, since that is run server-side.

Depending on how many files you attempt to send, and how large they are, the web request might time out. If you experience this, you could increase the timeout limit.

By doing this I was able to create an array of blobs in my web request body. I have also attached some pictures.


bilde

1 Like

Thank you for the thorough instructions!