如何解决红色节点在http-request节点中发送文件不起作用
我正在尝试通过http-request节点发送文件,但是它不起作用。
在请求正文节点中,添加了以下代码。
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-String
和a single Buffer Object
,但仍然遇到相同的错误
但是我尝试使用request模块在函数节点内部调用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节点的侧边栏文档中:
文件上传
要执行文件上传,应设置
msg.headers["content-type"]
到multipart/form-data
,并且传递到节点的msg.payload
必须 是具有以下结构的对象:{ "KEY": { "value": FILE_CONTENTS,"options": { "filename": "FILENAME" } } }
KEY,FILE_CONTENTS和FILENAME的值应设置为 适当的值。
按照此文档操作,您的msg.payload
是错误的,它应该类似于:
msg.payload: {
"name": "testName","description": "description","inputfile": {
"value": inputfile,"options": {
"filename": "filename.txt"
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。