文档插入REST api(POST / v1 / documents)需要multipart / mixed for content-type。所有在线示例都显示了如何使用multipart / form-data。根据我的研究,我了解到multipart / mixed需要嵌入multipart / form-data中。有人可以指点我的例子或资源,我可以得到一个线索?谢谢!
fyi:我在前端使用AngularJS,在后端使用Node.js
角色代码:
$http({
url: '/new',
method: 'POST',
headers: {
'Content-Type': undefined
},
transformRequest: function(data, getHeaders) {
var form = new FormData();
form.append('bug', angular.toJson(bug));
for (var i = 0; i < data.files.length; i++) {
console.log('FORM',data.files[i]);
form.append('file' + i, data.files[i]);
}
return form;
},
data: {
bug: bug,
files: $scope.files
}
}).success(function(data, status, headers, config) {
Flash.addAlert('success', 'successssss');
}).error(function(data, status, headers, config) {
Flash.addAlert('danger', 'failed');
});
节点代码::使用request 注意:这个代码肯定是错的,因为它将数据作为json而不是mulitpart / mixed,我不知道,因此问题
....
....
case 'POST':
console.log('its a POST');
var url = 'http://api-server.com:8003/v1/documents?extension=json';
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);
这是我目前获得的multipart / form-data
------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--
答案 0 :(得分:0)
JFY Content-Type: multipart/mixed
已弃用。
您应该改用 Content-Type: multipart/form-data
。
更多信息 - https://datatracker.ietf.org/doc/html/rfc7578
答案 1 :(得分:-1)
不同multipart/*
内容类型之间的差异只是语义,因此在将请求发送到REST端点之前,只需修改Content-Type
中的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);