如何在Node-RED中创建多部分HTTP请求 [英] How to create multipart HTTP request in Node-RED

查看:211
本文介绍了如何在Node-RED中创建多部分HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送包含以下表单数据的多部分请求:

I'm trying to send a multipart request with the following form-data:

------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--

以上数据通常是由Web服务创建和发送的.
我正在尝试从Node-RED复制完全相同的HTTP请求.
JSON数据已经准备好,因此只能创建多部分请求.

The above data is normally being created and sent by a web service.
I'm trying to replicate the exact same HTTP request from Node-RED.
JSON data is already prepared so it's just creation of the multipart request.

我尝试使用node-red-contrib-http-multipart,但我不确定如何配置它.

I've tried using node-red-contrib-http-multipart but I'm not really sure how to configure it.

[{"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":[]}]

任何有关如何解决该问题的指针将不胜感激.

Any kind of pointers on how to tackle the problem would be greatly appreciated.

推荐答案

似乎尚不支持直接在HTTP请求节点上直接创建multipart/form-data请求.但是,可以在功能节点上手动创建多部分有效负载,并将其提供给现成的HTTP请求节点.

It appears creating a multipart/form-data request directly on the HTTP request node is not yet supported. However, it's possible to manually create the multipart payload on a function node and feed it to the out-of-the-box HTTP request node.

基本上,将这样的有效负载提供给HTTP请求节点(来自 flows.nodered的摘录

Basically, feed to the HTTP request node a payload like so (a snippet from 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;

flows.nodered 中也提供了示例流.

The format of the multipart payload is thoroughly discussed in discourse.nodered.
A sample flow is also provided in flows.nodered.

这篇关于如何在Node-RED中创建多部分HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆