Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在http请求节点中发送文件的节点Red不工作_Node.js_Node Red - Fatal编程技术网

Node.js 在http请求节点中发送文件的节点Red不工作

Node.js 在http请求节点中发送文件的节点Red不工作,node.js,node-red,Node.js,Node Red,我正在尝试使用http请求节点发送文件,但它不工作 请查找节点红色流的下图。 在请求主体节点中,我添加了以下代码 const inputFile = msg.payload; const dataJson = { 'name': 'testName', 'description':'testdescription', 'inputfile': inputFile }; msg.payload = dataJson; msg.url = 'myAPIurl'; ms

我正在尝试使用http请求节点发送文件,但它不工作

请查找节点红色流的下图。

请求主体节点中,我添加了以下代码

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;
这是错误的请求错误

读取文件
节点中,我尝试选择了两个选项
一个UTF8字符串
一个缓冲区对象
,但仍然得到了相同的错误


但是我尝试使用请求模块调用函数节点内部的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请求
节点,我不确定我在哪里出错。

来自http请求节点的侧边栏文档:

文件上传

要执行文件上载,应设置
msg.headers[“content type”]
multipart/form data
,传递给节点的
msg.payload
必须 是具有以下结构的对象:

{
    "KEY": {
        "value": FILE_CONTENTS,
        "options": {
            "filename": "FILENAME"
        }
    }
}
键、文件内容和文件名的值应设置为 适当的值

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

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