How to send multiple files in a Custom App?

:bullseye: What is your goal?

send multiple files in my Custom App

:thinking: What is the problem & what have you tried?

I’d like to send multiple files to an external API using multipart/form-data in my integration.
I have already configured my mappable parameters to accept an array of files with the following structure:

{
  "name": "files",
  "type": "array",
  "label": "Files",
  "required": false,
  "spec": {
    "type": "collection",
    "spec": [
      {
        "name": "file_name",
        "type": "text",
        "label": "Name",
        "semantic": "file:name"
      },
      {
        "name": "file_data",
        "type": "buffer",
        "label": "Data",
        "semantic": "file:data"
      }
    ]
  }
}

and this in my communication section :

{
  "url": "https://webhook.site/770dbb14-a0a1-4552-81e0-b9de3bfaa7",
  "method": "POST",
  "type": "multipart/form-data",
  "qs": {
    "id": "{{parameters.id}}"
  },
  "body": {
    "TagList": "{{ stringify(parameterToObject(parameters.tagList))}}",
    "files": "{{parseFiles(parameters.files)}}"
  }
}

with the parseFiles IML function:

function parseFiles(files) {
  if (!files) return;
  return files.map(file => {
    return {
      value: file.data,
      options: {
        filename: file.filename
      }
    }
  })
}

The webhook receives the request, but the files are undefined in the formValues.

:clipboard: Error messages or input/output bundles

get undefined files

If anyone already encountered this problem it would really helped me!

The problem is in the parseFiles function, not in multipart itself. I hope that helps you!

I guess the problem is in the function but I have no idea how to solve that !

Thank’s for your help

1 Like

I asked different AI how could I solve that and they told me to use the following iml function :

function parseFiles(files) {
  if (!files || !Array.isArray(files)) return {};
  const result = {};
  files.forEach((file, index) => {
    if (file.file_data) {
      result[`file_${index}`] = {
        value: file.file_data,
        options: {
          filename: file.file_name
        }
      };
    }
  });
  return result;
}

but still it doesn’t work . I don ‘t even get the files at all

I found the answer !

it was a problem of wrong naming……….

function parseFiles(files) {
  if (!files) return;

  return files.map(file => {
    return {
      value: file.file_data,
      options: {
        filename: file.file_name
      }
    }
  })
}

I just changed file.data and file.filename to file.file_data and file.file_name (these are the name in the mappable parameters…)

Now it works !

2 Likes

I’m glad you made it work!