Node.js 在http请求节点中发送文件的节点Red不工作
我正在尝试使用http请求节点发送文件,但它不工作 请查找节点红色流的下图。 在请求主体节点中,我添加了以下代码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
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"
}
}
}