Node.js 如何将多部分/表单数据转换为多部分/混合

Node.js 如何将多部分/表单数据转换为多部分/混合,node.js,angularjs,forms,file-upload,Node.js,Angularjs,Forms,File Upload,文档insert REST api POST/v1/文档要求内容类型为multipart/mixed。所有联机示例都演示了如何使用多部分/表单数据。根据我的研究,我了解到multipart/mixed需要嵌入到multipart/form数据中。有人能给我指一个例子或资源,我可以从中得到线索吗?谢谢大家! 仅供参考:我在前端使用AngularJS,在后端使用Node.js 角度代码: 下面是我当前获取的多部分/表单数据 ------WebKitFormBoundaryJlYMd1KVllv1Wg

文档insert REST api POST/v1/文档要求内容类型为multipart/mixed。所有联机示例都演示了如何使用多部分/表单数据。根据我的研究,我了解到multipart/mixed需要嵌入到multipart/form数据中。有人能给我指一个例子或资源,我可以从中得到线索吗?谢谢大家!

仅供参考:我在前端使用AngularJS,在后端使用Node.js

角度代码:

下面是我当前获取的多部分/表单数据

------WebKitFormBoundaryJlYMd1KVllv1WgDS
Content-Disposition: form-data; name="bug"

{"relatedTo":[],"tickets":[],"id":1,"kind":"Other","createdAt":"2014-09-06T08:33:43.614Z","modifiedAt":"2014-09-06T08:33:43.614Z","status":"Verify","title":"Quae inventore beatae tempora mollit deserunt voluptatum odit adipisci consequat Est dolore quia perspiciatis","submittedBy":{"name":"Sudhakar Reddy","email":"sreddy@mycompany.com","username":"sreddy"},"assignTo":{"name":"Guzman Wagner","email":"guzmanwagner@mycompany.com","username":"small"},"description":"sdsdsdsds","category":"MLOS","tofixin":"Help-1.1","severity":"Performance","priority":{"level":"4","title":"Important"},"relation":"Test Specification task for","clones":[],"version":"6.0-3","platform":"EC2","memory":"Reprehenderit quia aut voluptatem in ex dolore eu numquam eum et esse officia id consequatur Est","processors":"Reiciendis nostrum adipisicing occaecat inventore veniam excepturi","note":"Officiis qui adipisci commodo eveniet, esse aperiam est non unde possimus, sed nesciunt, exercitation eius magna consequat. Sint ipsa, laboriosam.","changeHistory":[],"subscribers":[{"name":"Sudhakar Reddy","email":"sreddy@mycompany.com","username":"sreddy"},{"name":"Guzman Wagner","email":"guzmanwagner@mycompany.com","username":"small"}],"attachments":[{"webkitRelativePath":"","lastModifiedDate":"2014-07-18T23:53:29.000Z","name":"jamesbond.jpg","type":"image/jpeg","size":858159}]}
------WebKitFormBoundaryJlYMd1KVllv1WgDS
Content-Disposition: form-data; name="file0"; filename="jamesbond.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryJlYMd1KVllv1WgDS--

不同的multipart/*内容类型之间的区别只是语义上的,因此只需在将请求发送到REST端点之前修改req.headers中的内容类型即可:

var url = 'http://api-server.com:8003/v1/documents?extension=json';

// if you use `req.headers` elsewhere, you may want to make a copy of the
// headers object so as not to mutate the original headers ...
req.headers['content-type'] = req.headers['content-type']
                                 .replace('multipart/form-data',
                                          'multipart/mixed');

var options = {
    method: 'POST',
    headers: req.headers,
    url: url,
    body: JSON.parse(req.body.bug),
    json: true
};

req.pipe(request(options, function(error, response, body) {
    if (error) {
        next(error);
    }
})).pipe(res);

使用解决方案后,不会提交请求。chrome debugger中的标题显示了警告:显示了临时标题,那么如何为每个部分设置内容处置标题例如:我需要这样的内容:内容处置:附件;filename=doc2.json,但目前我不知道如何更改它
------WebKitFormBoundaryJlYMd1KVllv1WgDS
Content-Disposition: form-data; name="bug"

{"relatedTo":[],"tickets":[],"id":1,"kind":"Other","createdAt":"2014-09-06T08:33:43.614Z","modifiedAt":"2014-09-06T08:33:43.614Z","status":"Verify","title":"Quae inventore beatae tempora mollit deserunt voluptatum odit adipisci consequat Est dolore quia perspiciatis","submittedBy":{"name":"Sudhakar Reddy","email":"sreddy@mycompany.com","username":"sreddy"},"assignTo":{"name":"Guzman Wagner","email":"guzmanwagner@mycompany.com","username":"small"},"description":"sdsdsdsds","category":"MLOS","tofixin":"Help-1.1","severity":"Performance","priority":{"level":"4","title":"Important"},"relation":"Test Specification task for","clones":[],"version":"6.0-3","platform":"EC2","memory":"Reprehenderit quia aut voluptatem in ex dolore eu numquam eum et esse officia id consequatur Est","processors":"Reiciendis nostrum adipisicing occaecat inventore veniam excepturi","note":"Officiis qui adipisci commodo eveniet, esse aperiam est non unde possimus, sed nesciunt, exercitation eius magna consequat. Sint ipsa, laboriosam.","changeHistory":[],"subscribers":[{"name":"Sudhakar Reddy","email":"sreddy@mycompany.com","username":"sreddy"},{"name":"Guzman Wagner","email":"guzmanwagner@mycompany.com","username":"small"}],"attachments":[{"webkitRelativePath":"","lastModifiedDate":"2014-07-18T23:53:29.000Z","name":"jamesbond.jpg","type":"image/jpeg","size":858159}]}
------WebKitFormBoundaryJlYMd1KVllv1WgDS
Content-Disposition: form-data; name="file0"; filename="jamesbond.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryJlYMd1KVllv1WgDS--
var url = 'http://api-server.com:8003/v1/documents?extension=json';

// if you use `req.headers` elsewhere, you may want to make a copy of the
// headers object so as not to mutate the original headers ...
req.headers['content-type'] = req.headers['content-type']
                                 .replace('multipart/form-data',
                                          'multipart/mixed');

var options = {
    method: 'POST',
    headers: req.headers,
    url: url,
    body: JSON.parse(req.body.bug),
    json: true
};

req.pipe(request(options, function(error, response, body) {
    if (error) {
        next(error);
    }
})).pipe(res);