Upload PDF through function node returns 500 error

Hey all!!
I am trying to upload PDF attachment by using function node. Basically, I got the base64 string of a PDF file from web hook. I will convert it and upload. Initially I used http request node but I got timeout exceeded error. So, I decided to use function node to upload the pdf by using axios. I installed axios and form-data node modules. But when I try to upload PDF, I got 500 error. Can anyone help me? What I am doing wrong.

Check below

Message": "Request failed with status code 500",
"name": "Error",
"stack": "Error: Request failed with status code 500 at createError (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/createError.js:16:15) at settle (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/adapters/http.js:269:11) at IncomingMessage.emit (node:events:538:35) at endReadableNT (node:internal/streams/readable:1345:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)",

My Workflow



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

const file = $node["Set1"].json;
const url = $node["appConfig"].json.envUrl+'/orgs/'+$node["appConfig"].json["orgKey"]+'/projects/'+$node["appConfig"].json["projectKey"]+'/activities/'+$node["payload"].json["adds"][0]["activityKey"]+'/pdfs'
const token = $node["Auth_Token"].json["token"];


const metadata = {
    activityId:$node["payload"]?.json?.adds?.[0]?.activityTypeActivityTypeId,
    idempotencyKey:$node["Attachment_Payload"]?.json?.idempotencyKey,
    sha1:$node["SHA1"]?.json,
};


var data = new FormData();
data.append('pdf', Buffer.from(file.pdf, 'base64'));
data.append('activityId', metadata.activityId);
data.append('idempotencyKey', metadata.idempotencyKey);
data.append('sha1', metadata.sha1);

var config = {
  method: 'post',
  url: url,
  headers: { 
    'content-type': 'multipart/form-data',
    'Authorization': `Bearer ${token}`,
  },
  maxContentLength: 10000000,
  maxBodyLength: 10000000,
  data : data
};

console.log(config);

const result = await axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
  let respData = [];
  respData.push(response.data);
  return [{'respData': respData}];
})
.catch(function (error) {
  console.log(error);
  return [{'failed': error}];
});

return [{'result': result}];

Share the output returned by the last node

The file upload node is last one. It returns the same error which I shared.

Hi @Siva_Nagarajan, I am sorry to hear you’re having trouble here. On first sight this looks like a problem with your code tbh - is the snippet working outside of n8n?

And can you share the API documentation for the service you’re uploading the file to?

@MutedJam Thanks for your response. I haven’t tried outside of n8n. But upload PDF functionality is working fine in our web application. Unfortunately we are not using axios there.

Yes I can share API documentation info. Please check the screenshot for API documentation

If you see the screen shot, we need to pass activityId, IdempotencyKey, pdf and sha1 hash value as form data.

As I said before we used to get base64 string of pdf.

Hope, I shared the enough information.