如何在 Node-RED 中创建多部分 HTTP 请求
How to create multipart HTTP request in Node-RED
我正在尝试发送具有以下表单数据的多部分请求:
------WebKitFormBoundaryjFqPRXY6RQpdTRvE
Content-Disposition: form-data; name="file"; filename="Sample.csv"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundaryjFqPRXY6RQpdTRvE
Content-Disposition: form-data; name="data"; filename="blob"
Content-Type: application/json
{"name":"Sample5","type":"Csv","firstRowIsHeader":true,"columns":[{ ... }]}
------WebKitFormBoundaryjFqPRXY6RQpdTRvE--
以上数据通常由网络服务创建和发送。
我正在尝试从 Node-RED 复制完全相同的 HTTP 请求。
JSON 数据已经准备好,所以它只是创建多部分请求。
我试过使用 node-red-contrib-http-multipart
但我不太确定如何配置它。
[{"id":"e0fde1bd.e0aa","type":"httpInMultipart","z":"fc689d44.1c52","name":"","url":"/test/upload","method":"post","fields":"[ { \"name\": \"file\", \"maxCount\": 1}, { \"name\": \"data\", \"maxCount\": 1} ]","swaggerDoc":"","x":1390,"y":820,"wires":[["d109ed84.14d1d","8a39aaa0.6934c8","d0bc5b20.45f3a8"]]},{"id":"a2318dfd.bae7d","type":"http in","z":"fc689d44.1c52","name":"","url":"/test/send","method":"get","upload":false,"swaggerDoc":"","x":1380,"y":680,"wires":[["d7cfc418.89eb98"]]},{"id":"14c992f3.8652ad","type":"http response","z":"fc689d44.1c52","name":"","x":1750,"y":680,"wires":[]},{"id":"d109ed84.14d1d","type":"debug","z":"fc689d44.1c52","name":"","active":true,"console":"false","complete":"true","x":1570,"y":780,"wires":[]},{"id":"8a39aaa0.6934c8","type":"http response","z":"fc689d44.1c52","name":"","statusCode":"","headers":{},"x":1570,"y":860,"wires":[]},{"id":"1a646c83.8da7d3","type":"debug","z":"fc689d44.1c52","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1790,"y":780,"wires":[]},{"id":"d7cfc418.89eb98","type":"template","z":"fc689d44.1c52","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<form action=\"/test/upload\" method=\"POST\" enctype=\"multipart/form-data\">\n <div>\n <input type=\"file\" name=\"file\">\n <input type=\"submit\" value=\"Submit\">\n </div>\n</form>","output":"str","x":1580,"y":680,"wires":[["2971b104.12737e","14c992f3.8652ad"]]},{"id":"d0bc5b20.45f3a8","type":"change","z":"fc689d44.1c52","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"file\": req.files.file[0].buffer,\t \"data\": \"test\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1620,"y":820,"wires":[["1a646c83.8da7d3"]]},{"id":"2971b104.12737e","type":"debug","z":"fc689d44.1c52","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1730,"y":640,"wires":[]}]
任何关于如何解决问题的指示都将不胜感激。
您不再需要多部分贡献节点。一段时间以来,开箱即用的 http 输入节点已经能够处理多部分文件上传。
将方法设置为POST
并勾选file uploads
选项。文件可以在 message.req.files
.
中找到
如果您希望从 Node-RED 向某些服务发送多部分 POST 请求,请使用 HTTP 请求节点。如何在节点信息面板中。复制在下面,但最好检查节点信息面板本身,因为下面的文本可能会过时 -
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.
这是一个示例流程:
[{"id":"f7b0ae63.e74e5","type":"fileinject","z":"aacbc9d.6412c38","name":"","x":140,"y":160,"wires":[["5cb04348.8b9e2c"]]},{"id":"5cb04348.8b9e2c","type":"function","z":"aacbc9d.6412c38","name":"","func":"msg.headers = {\n \"content-type\" : 'multipart/form-data'\n };\nlet databuffer = msg.payload;\n\nmsg.payload = {\n \"KEY\": {\n \"value\": databuffer,\n \"options\": {\n \"filename\": \"myfile.png\"\n }\n }\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":160,"wires":[["f04784b6.9e3078"]]},{"id":"f04784b6.9e3078","type":"http request","z":"aacbc9d.6412c38","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"myserver.mybluemix.net/file/input","tls":"","proxy":"","authType":"basic","x":450,"y":160,"wires":[["76de17b4.dc2de8"]]},{"id":"76de17b4.dc2de8","type":"debug","z":"aacbc9d.6412c38","name":"File has been sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":160,"wires":[]}]
似乎尚不支持直接在 HTTP 请求节点上创建 multipart/form-data 请求。但是,可以在函数节点上手动创建多部分负载并将其提供给 out-of-the-box HTTP 请求节点。
基本上,向 HTTP 请求节点提供这样的有效负载(来自 flows.nodered 的片段):
msg.headers = {
"Content-Type": "multipart/form-data; boundary=------------------------d74496d66958873e"
}
msg.payload = '--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="select"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="print"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="file"; filename="'+msg.filename+'"\r\n'+
'Content-Type: application/octet-stream\r\n'+
'\r\n'+
msg.payload+'\r\n'+
'--------------------------d74496d66958873e--\r\n';
return msg;
discourse.nodered 中详细讨论了多部分有效负载的格式。
flows.nodered.
中还提供了示例流程
我正在尝试发送具有以下表单数据的多部分请求:
------WebKitFormBoundaryjFqPRXY6RQpdTRvE
Content-Disposition: form-data; name="file"; filename="Sample.csv"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundaryjFqPRXY6RQpdTRvE
Content-Disposition: form-data; name="data"; filename="blob"
Content-Type: application/json
{"name":"Sample5","type":"Csv","firstRowIsHeader":true,"columns":[{ ... }]}
------WebKitFormBoundaryjFqPRXY6RQpdTRvE--
以上数据通常由网络服务创建和发送。
我正在尝试从 Node-RED 复制完全相同的 HTTP 请求。
JSON 数据已经准备好,所以它只是创建多部分请求。
我试过使用 node-red-contrib-http-multipart
但我不太确定如何配置它。
[{"id":"e0fde1bd.e0aa","type":"httpInMultipart","z":"fc689d44.1c52","name":"","url":"/test/upload","method":"post","fields":"[ { \"name\": \"file\", \"maxCount\": 1}, { \"name\": \"data\", \"maxCount\": 1} ]","swaggerDoc":"","x":1390,"y":820,"wires":[["d109ed84.14d1d","8a39aaa0.6934c8","d0bc5b20.45f3a8"]]},{"id":"a2318dfd.bae7d","type":"http in","z":"fc689d44.1c52","name":"","url":"/test/send","method":"get","upload":false,"swaggerDoc":"","x":1380,"y":680,"wires":[["d7cfc418.89eb98"]]},{"id":"14c992f3.8652ad","type":"http response","z":"fc689d44.1c52","name":"","x":1750,"y":680,"wires":[]},{"id":"d109ed84.14d1d","type":"debug","z":"fc689d44.1c52","name":"","active":true,"console":"false","complete":"true","x":1570,"y":780,"wires":[]},{"id":"8a39aaa0.6934c8","type":"http response","z":"fc689d44.1c52","name":"","statusCode":"","headers":{},"x":1570,"y":860,"wires":[]},{"id":"1a646c83.8da7d3","type":"debug","z":"fc689d44.1c52","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1790,"y":780,"wires":[]},{"id":"d7cfc418.89eb98","type":"template","z":"fc689d44.1c52","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<form action=\"/test/upload\" method=\"POST\" enctype=\"multipart/form-data\">\n <div>\n <input type=\"file\" name=\"file\">\n <input type=\"submit\" value=\"Submit\">\n </div>\n</form>","output":"str","x":1580,"y":680,"wires":[["2971b104.12737e","14c992f3.8652ad"]]},{"id":"d0bc5b20.45f3a8","type":"change","z":"fc689d44.1c52","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"file\": req.files.file[0].buffer,\t \"data\": \"test\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1620,"y":820,"wires":[["1a646c83.8da7d3"]]},{"id":"2971b104.12737e","type":"debug","z":"fc689d44.1c52","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1730,"y":640,"wires":[]}]
任何关于如何解决问题的指示都将不胜感激。
您不再需要多部分贡献节点。一段时间以来,开箱即用的 http 输入节点已经能够处理多部分文件上传。
将方法设置为POST
并勾选file uploads
选项。文件可以在 message.req.files
.
如果您希望从 Node-RED 向某些服务发送多部分 POST 请求,请使用 HTTP 请求节点。如何在节点信息面板中。复制在下面,但最好检查节点信息面板本身,因为下面的文本可能会过时 -
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.
这是一个示例流程:
[{"id":"f7b0ae63.e74e5","type":"fileinject","z":"aacbc9d.6412c38","name":"","x":140,"y":160,"wires":[["5cb04348.8b9e2c"]]},{"id":"5cb04348.8b9e2c","type":"function","z":"aacbc9d.6412c38","name":"","func":"msg.headers = {\n \"content-type\" : 'multipart/form-data'\n };\nlet databuffer = msg.payload;\n\nmsg.payload = {\n \"KEY\": {\n \"value\": databuffer,\n \"options\": {\n \"filename\": \"myfile.png\"\n }\n }\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":160,"wires":[["f04784b6.9e3078"]]},{"id":"f04784b6.9e3078","type":"http request","z":"aacbc9d.6412c38","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"myserver.mybluemix.net/file/input","tls":"","proxy":"","authType":"basic","x":450,"y":160,"wires":[["76de17b4.dc2de8"]]},{"id":"76de17b4.dc2de8","type":"debug","z":"aacbc9d.6412c38","name":"File has been sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":160,"wires":[]}]
似乎尚不支持直接在 HTTP 请求节点上创建 multipart/form-data 请求。但是,可以在函数节点上手动创建多部分负载并将其提供给 out-of-the-box HTTP 请求节点。
基本上,向 HTTP 请求节点提供这样的有效负载(来自 flows.nodered 的片段):
msg.headers = {
"Content-Type": "multipart/form-data; boundary=------------------------d74496d66958873e"
}
msg.payload = '--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="select"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="print"\r\n'+
'\r\n'+
'true\r\n'+
'--------------------------d74496d66958873e\r\n'+
'Content-Disposition: form-data; name="file"; filename="'+msg.filename+'"\r\n'+
'Content-Type: application/octet-stream\r\n'+
'\r\n'+
msg.payload+'\r\n'+
'--------------------------d74496d66958873e--\r\n';
return msg;
discourse.nodered 中详细讨论了多部分有效负载的格式。
flows.nodered.