Upload File: Multipart-formdata

Nice idea @Kirst , just tested it and yes. Check the example below. Keep in mind that in order to require an NPM package on the function node you have to set an env variable. By default require a module is not allowed. In the example, I used the request-promise-native module and told N8N to use it like this export NODE_FUNCTION_ALLOW_BUILTIN=request-promise-native. More info about that here.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const request = require('request-promise-native');\n\nconst binaryData = items[0].binary.data;\n\nconst metadata = {\n  name: binaryData.fileName,\n  parent: { id: 123 }\n}\n\nconst options = {\n  uri: 'https://internal.users.n8n.cloud/webhook-test/2/webhook/webhook',\n  headers: {\n    'content-type': 'multipart/form-data',\n  },\n  formData: {\n    file: {\n      value: Buffer.from(binaryData.data, 'base64'),\n      options: {\n\t    filename: binaryData.fileName,\n\t    contentType: binaryData.mimeType,\n\t  },\n \t},\n    attributes: JSON.stringify(metadata),\n  },\n  method: 'POST',\n}\n\nawait request(options);\n\nreturn [{ json: {} }]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        670,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://i.insider.com/5df126b679d7570ad2044f3e?width=1100&format=jpeg&auto=webp",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        470,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
4 Likes