Axios Request Error ("split")

Describe the issue/error/question

For my workflow i need to make a multipart/form-data request. I found some examples on the forum and put together the snippets. However the last function node always returns: “ERROR: Cannot read properties of undefined (reading ‘split’)” and i have no clue why.

What is the error message (if any)?

ERROR: Cannot read properties of undefined (reading ‘split’)

TypeError: Cannot read properties of undefined (reading 'split')
    at Object.execute (/app/code/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:127:91)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Workflow.runNode (/app/code/node_modules/n8n-workflow/dist/src/Workflow.js:594:28)
    at async /app/code/node_modules/n8n-core/dist/src/WorkflowExecute.js:557:53

Please share the workflow

Information on your n8n setup

  • n8n version: 0.167.2
  • Database you’re using (default: SQLite): Postgresql
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker/Cloudron

Hi @JUVOJustin,
you are not doing any split in your function node, so the error may happen outside of your code. In your function node you do not return any data. Can you try adding return items; to the end?

Sadly the same result. Same error. Ideally, i would like to return the response result later to check if the submit worked. However right now the request fails in the first place.

Does it work if you comment out the actual http request?

const response = await this.helpers.httpRequest(config);
console.log(response);

With the following code i receive “ERROR: Cannot read properties of undefined (reading ‘every’) [Line 852]”

var axios = require('axios');
var FormData = require('form-data');

// Get all currently existing binary data
const binaryData = items[0].binary.data

var data = new FormData();
data.append('file', Buffer.from(binaryData.data, 'base64'));
data.append('type', 'voucher');

var config = {
  url: 'https://api.lexoffice.io/v1/files',
  headers: { 
    'Authorization': 'Bearer ******',
    'Content-Type': 'multipart/form-data'
  },
  method: 'POST',
  body: data, 
};

console.log(config)

//const response = await this.helpers.httpRequest(config);
//console.log(response);

But the console.log of config was executed.

I created a simplified version of your workflow with n8n v0.179.0. I am getting the same error

if I do not return items at the end. Without the http request and with returning items it works.

So the problem should be the binary buffer when doing a http request. :upside_down_face:

1 Like

Can you try surrounding the http request with try/catch

In my case (without working bearer token) I at least got a better error message 401 unauthorized

The function node does execute when i return items without executing the request.

When executing the request i get the following error (try&catch):

Also tried the dummy file you used with the exact same http node to w3. Replaced the functions to functionItem to use getBinaryData() in case there is something going on i am not aware of. As you said when returning items/item the node executes but with for the request i still get an error 500 no matter which file it submits.

1 Like

According to lexoffice’s files-endpoint-upload-a-file a 500 error means

If no file is provided, the file upload endpoint returns HTTP status 500

I sadly cannot try out lexoffice’s api. Can you try out not setting the Content-Type header, the request helper should set it automatically if you use FormData.

1 Like

Ok got it. You are absolutely right. It is about the endpoint. I am very sorry i did not check that :frowning:
Sending the data to requestBin works: RequestBin.com — A modern request bin to collect, inspect and debug HTTP requests and webhooks

I know it is a lot to ask, but do you maybe have an idea why the lexoffice endpoint fails? My understanding of the binary stuff is not that great and i am wondering if it is correct to only sending the “data” without the other file info to lexoffice. Coming from integromat/make i was used to creating the form entries via the ui, and formatting was done automagically.

Yeah i also think it might have something to do with missing filename. Could you try out something like

data.append('file', Buffer.from(binaryData.data, 'base64')`, 'filename.pdf');

Still error 500 :frowning:

Can you try setting headers like this

Thank you very much. I solved it. Here is my working code in case anyone needs the same thing. Definitely looking forward to an integrated way to mix form requests :smiley:

var axios = require('axios');
var FormData = require('form-data');

// Get all currently existing binary data
const binaryData = getBinaryData().data
var data = new FormData();
data.append('file', Buffer.from(binaryData.data, 'base64'), item.filename);
data.append('type', 'voucher');

var config = {
  url: 'https://api.lexoffice.io/v1/files',
  headers: { 
    'Authorization': 'Bearer *****',
    ...data.getHeaders()
  },
  method: 'POST',
  body: data, 
};

try {
  await this.helpers.httpRequest(config);
  return item;
} catch(ex) {
  return ex;
}

I dont know why put passing the filename from “data” caused error 400. Passing the “real” filename from zammad works for whatever reason. Don’t know what on earth lexoffice is checking there :smiley:

However it works and i have to thank you very much.

1 Like