mkHun
mkHun

Reputation: 5927

Node-Red sending the file in http-request node is not working

I am trying to send the file with the http-request node but it is not working.

Please find the following image of Node-red flow. enter image description here

In the Request Body node I have added the following code.

const inputFile = msg.payload;

const dataJson = 
{
    'name': 'testName',
    'description':'testdescription',
    'inputfile': inputFile
};
msg.payload = dataJson;
msg.url = 'myAPIurl';

msg.headers = {
    'authorization': 'Bearer TOKEN Here',
    'cookie': 'Cookie here',
    'content-type': 'multipart/form-data;'
};

return msg;

This is giving bad request error.

In the Read File node I tried choosing both options A single UTF8-String and a single Buffer Object still I got the same error


But I tried to call the API inside function node using the request module. It is giving the proper response.

const request = global.get("request");
const fs = global.get("fs");

const url = 'API';

const tkn = 'TOken Here';
const cookie = 'cookie here';

const fl = fs.createReadStream('/tmp/node-red/app/data/filename.txt');
var options = {
    method: 'POST',
    url: url,
    headers: {
        'Authorization': tkn,
        'Cookie': cookie,
    },
    formData: {
        "name": "test121",
        "description": "",
        inputfile: fl
    }
};

request(options, function (err, resp, body) {

    console.log(body);

});

return msg;

I am not sure where I am making the mistake if I use http-request node.

Upvotes: 0

Views: 2904

Answers (1)

hardillb
hardillb

Reputation: 59731

From the sidebar docs for the http-request node:

File Upload

To perform a file upload, msg.headers["content-type"] should be set to multipart/form-data and the msg.payload passed to the node must be an object with the following structure:

{
    "KEY": {
        "value": FILE_CONTENTS,
        "options": {
            "filename": "FILENAME"
        }
    }
}

The values of KEY, FILE_CONTENTS and FILENAME should be set to the appropriate values.

Following this doc, your msg.payload is wrong, it should look something like:

msg.payload: {
  "name": "testName",
  "description": "description",
  "inputfile": {
    "value": inputfile,
    "options": {
      "filename": "filename.txt"
    }
  }
}

Upvotes: 1

Related Questions