通过requestJS POST请求发送JSON对象和图像文件的数组

我正在使用NODEjs构buildREST API,使用Express路由器和Multer中间件来处理多个主体数据和文件。

我的端点路由127.0.0.1/api/postData期望:带有字段的json数据,其中之一是json对象数组(我有嵌套mongoose模式)和2个命名图像(png / jpg)。

我需要通过cURL发送Post请求与以下5对象的数据结构:

name String description String usersArray Array of json objects like: [{"id": "123"}, {"id": "456}] imgIcon Png/Image providing /path/to/imageIcon.png imgHeader Png/Image providing /path/to/imageHeader.png 

任何想法如何写request.js节点http请求库的帮助下请求库?

尝试以下操作:

 request.post({ url:'http://127.0.0.1:7777/api/postData' , formData: formData , qsStringifyOptions : { arrayFormat : 'brackets' // [indices(default)|brackets|repeat] } }, function (err, httpResponse, body) { // do something... } 

我在https://www.npmjs.com/package/qs (由https://www.npmjs.com/package/request使用 )中发现了三个arrayFormat选项:

 'indices' sends in postbody: (this is the default case) usersArray%5B0%5D%5Bid%5D=a667cc8f&usersArray%5B1%5D%5Bid%5D=7c7960fb decoded: usersArray[0][id]=a667cc8f&usersArray[1][id]=7c7960fb 'brackets' sends in postbody: usersArray%5B%5D%5Bid%5D=a667cc8f&usersArray%5B%5D%5Bid%5D=7c7960fb decoded: usersArray[][id]=a667cc8f&usersArray[][id]=7c7960fb 'repeat' sends in postbody: usersArray%5Bid%5D=a667cc8f&usersArray%5Bid%5D=7c7960fb decoded: usersArray[id]=a667cc8f&usersArray[id]=7c7960fb 

这些是在发布之前序列化数组的三种不同方法。 基本上它取决于接收端如何这些需要/可以格式化。 在我的情况下,它有助于使用“括号”

我试图找出这个HTTPpost。 这是我使用requestJS节点包的当前版本:

 var request = require('request'); var formData = { name: 'LEGO', description: 'LEGO is Danish privately held company engaged in the production series of educational toys which are a set of parts to build and simulate a variety of life and technical objects and machines.', // Pass users Array usersArray: [{"id": "a667cc8f"}, {"id": "7c7960fb"}], // Attach Icon and Header image files imgIcon: '/var/tmp/img/LEGO_icon.png', imgHeader: '/var/tmp/img/LEGO_header.png' }; request.post({url:'http://127.0.0.1:7777/api/postData', formData: formData}, function (err, httpResponse, body) { if (err) { return console.error('failed:', err); } console.log('Post request is successful! Server responded with:', body); }); 

但是我运行它时遇到了这个错误:

 $ /home/username/dev/myproject/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33 source.on('error', function() {}); 

我提供用户数组的方式有点不对,但是我不能把手指怎么做。