Node-Red 在 http-request 节点中发送文件不起作用

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

我正在尝试使用 http-request 节点发送文件,但它不起作用。

请找到下图的Node-red flow。

Request Body 节点中,我添加了以下代码。

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;

这是错误的请求错误。

Read File 节点中,我尝试选择 A single UTF8-Stringa single Buffer Object 这两个选项,但我仍然遇到相同的错误


但我尝试使用请求模块调用API 内部函数节点。它给出了正确的响应。

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;

如果我使用 http-request 节点,我不确定我在哪里犯了错误。

来自 http-request 节点的边栏文档:

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.

按照此文档,您的 msg.payload 是错误的,它应该类似于:

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