Help for the noob to upload a file using function node

Hi guys,

After of the all…thanks for this excellent project. N8N is amazing!!!

I searched a lot to find a solution but I didn’t found anything. I followed the tips from the topic:
Upload CSV with HTTP Request

I’m use the latest version (0.182.0) and my workflow is very simple, I need get a xml file from Nessus Scan API and import this file in DefectDojo API. All workflow works except the file upload…

Follow print error:

Follow the details errors:

TypeError: Cannot read properties of undefined (reading 'split')
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:127:91)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:594:28)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:537:49

Follow the javascript code of the function node:

const request = require('request-promise-native');
const binaryData = items[0].binary.data;
const data = await this.helpers.getBinaryDataBuffer(0, 'data');

const options = {
  uri: 'http://defectdojo.homolog.jm:8080/api/v2/import-scan/',
  headers: {
    'content-type': 'multipart/form-data',
    'Authorization': 'Token xxx',
  },
  formData: {
    file: {
      value: Buffer.from(data, 'base64'),
      options: {
        filename: binaryData.fileName,
        contentType: binaryData.mimeType,
	  },
 	},
    minimum_severity: 'Info',
    active: 'true',
    verified: 'true',
    scan_type: 'Nessus Scan',
    product_name: 'ProductName',
    engagement_name: 'EngagementName',
    close_old_findings: 'true',
    push_to_jira: 'false',
    environment: 'Production',
  },
  method: 'POST',
}

await request(options);

return [{ json: {} }]

I don’t know where is my error…please anybody could you help me? Or any other way to do that?

PS: I don’t know with have some relation with the problem…but I can see the xml content when I download the binary but when I click on “View Button” I can’t see nothing…

Hi @iamoshiro, I am sorry to hear you are having trouble. The example code you have provided is working fine for me when testing this though:

Both file and the form data arrive on the destination server as expected:

Any chance you can confirm which version of n8n exactly you are using and share a workflow using which this problem can be reproduced?

Hi @MutedJam.

Thanks for your answer, I’m using the latest version (0.182.0).

I did new tests make a POST request to list the users in DefectDojo api and the same error persisted.

Follow the print error and details:

TypeError: Cannot read properties of undefined (reading 'split')
    at Object.execute (/usr/local/lib/node_modules/n8n/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 (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:594:28)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:537:49

Also follow the code:

const request = require('request-promise-native');


const options = {
  url: 'http://defectdojo.homolog.jm:8080/api/v2/users',
  headers: {
    'Authorization': 'Token xxx',
  },
  method: 'POST',
  rejectUnauthorized: false
}

await request(options);

return [{ json: {} }]

But when I used the HTTP request node for list users it’s works:

PS: For the second tests I’m using only the function node and request node (separed):

I’ll try new tests using the others versions…

I tried some tests using other versions below and the error continued so I suspected the problem was in request-promise-native.

So I did some testing using axios and the POST request to list users and works fine:

I’m investigating way this happens just to request-promise native, but I noted maybe that the problem in the “await request(options);” line but I don’t have certain.

Hi @MutedJam

Please help me…I don’t know what to do…the module “request-promise-native” didn’t work with me…so I decided to use the Axios, the get request works well.

But my problem is to construct the post request, I tested using Postman and all it’s fine, but when I try to replicate this the function node isn’t working, I think is something I’m forgetting to do…

Follow the Postman code snippet (NodeJs-Axios):

var axios = require('axios');
var FormData = require('form-data');
var data = new FormData();
data.append('target_start', '2022-06-17');
data.append('target_end', '2022-06-17');
data.append('product', '2');
data.append('name', 'Teste001');

var config = {
  method: 'post',
  url: 'http://localhost:8080/api/v2/engagements/',
  headers: { 
    'Authorization': 'Token 3aa2667ad28e326e1712aced09310e0ccac1a16f', 
    'Cookie': 'csrftoken=qJ6JYSL2TVWSxduPBBhBhSXXMpOGw9wpbhsKTBGcfZWph851s3dFztkO6JzmG8aE', 
    ...data.getHeaders()
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Follow my function code:

const axios = require('axios');
const token = 'Token 3aa2667ad28e326e1712aced09310e0ccac1a16f'
url = 'http://192.168.100.5:8080/api/v2/engagements/'
const FormData = require('form-data');
const data = new FormData();
data.append('name', 'Teste003');
data.append('target_start', '2022-06-17');
data.append('target_end', '2022-06-17');
data.append('product', '2');

try {
    const response = await axios.post(url, data, {headers: {'Authorization': `${token}`}})
    console.log(response.data);
    return [{ json:response.data }] 
  } catch (error) {
    console.log(error.response.body);
    return [{ json: {'value':'error'} }] 
  }

Follow the workflow too:

Don’t worry about the API Keys it’s a lab environment.

Best Regards,

Hey @iamoshiro, this looks like a problem with n8n error handling that would require a closer look.

In the meantime, can you try to adjust your code like so?

const request = require('request-promise-native');


const options = {
  url: 'http://defectdojo.homolog.jm:8080/api/v2/users',
  headers: {
    'Authorization': 'Token xxx',
  },
  method: 'POST',
  rejectUnauthorized: false
}

try {
  await request(options);
  return [{ json: {} }];
} catch (e) {
  return [{ json: {
      error: JSON.stringify(e)
    }
  }];
}

This should return a more helpful error message like so:

In this example it’s the defectdojo.homolog.jm domain not being found, but in your network the problem might be different one.

Workflow:

Hi @MutedJam

About the get request purpose the error is gone, but isn’t return the json results:

For other side the post request isn’t work:

Follow the post request from Postman:

PS. Using the Axios the same happens, the get request works and returns the result but when I use post request doesn’t work (but it’s the case I think the cause it’s me).

Could you try adding a trailing slash to your URL? From looking at the error you can see that your server responds with a 301 status and a location header telling you the URL ends with .../users/, but in your request you’re using .../users.

Postman follows these redirects automatically from the looks of it, but your code doesn’t.

@MutedJam

Ohhh my apologies, you’re right. I forgot the slash…the post request is working now, but the get request doesn’t show the results yet.

Hey @MutedJam

Everything works now, thanks very much man for your help. That’s why I love N8N!

Regards.

2 Likes

Glad to hear, thank so much for confirming!

1 Like