Hey,
I've done my first steps with Node Red and got some experiences with it.
But now I stuck on the Problem, that the file won't be send to the API.
It seems the File-Content is missing.
But let's start from the beginning... what I have so far:
First of all, I have a flow, which parse my incomming Mails to find Emails, which are invoices and have an attachment.
(This part work like a charm).
It looks like this:
So far, so good. There you see the content of the file is an buffer with the Data.
So let's jump to the next flow, who should send this file to a REST-API. Thats what it looks like:
The Function-Node "API-Template" contain the following source-code.
Because of try & error, I tried two ways to construct the Payload:
FIRST (full written):
const { payload: {attachment} } = msg;
node.warn(msg)
const body = '----AaB03x\r\n'+
'Content-Disposition: form-data; name="type"\r\n'+
'voucher\r\n'+
'----AaB03x\r\n'+
'Content-Disposition: form-data; name="file"; filename="'+attachment.filename.replace(" ","")+'"\r\n'+
'Content-Type: '+attachment.contentType+'\r\n'+
'Content-Transfer-Encoding: binary\r\n'+
attachment.content+'\r\n'+
'\r\n'+
'----AaB03x\r\n';
const headers = {};
headers["Content-Type"]="multipart/form-data; boundary=--AaB03x";
headers["Authorization"]="Bearer <my-secret-token-stay-here>";
headers["Accept"]="application/json";
headers["Content-Length"]=body.length.toString();
return {payload:body, headers};
The Request for the First Try looks like this:
SECOND (as Object):
const { payload: {attachment} } = msg;
node.warn(msg)
msg.headers = {
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer <my-secret-token-stay-here>',
'Content-Transfer-Encoding':'binary',
'Content-Length': attachment.content.length.toString()
};
msg.payload = {
'file' : {
'value': attachment.content.toString('binary'),
'options': {
'filename': attachment.filename.replace(" ",""),
},
'type':'voucher'
},
}
return msg
The Request for Second-Try looks like this:
But the API-Response for both requests is: statusCode: 500
.
API-Documentation notice: If no file is provided, the file upload endpoint returns HTTP status 500 .
BTW: The above Requests are piped to http-request
node which do a POST
to the API-Endpoint.
So the question at all: What's wrong with the way I send the file-content. Did I miss something?