The document insert REST api (POST /v1/documents) requires multipart/mixed for content-type. All the examples online show how to use multipart/form-data. Based on my research I learned that multipart/mixed needs to be embedded in multipart/form-data. Can someone please point me to a example or resource where I can get a clue ? Thank you!
fyi: I'm using AngularJS on the front end and Node.js on the backend
Angular code:
$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');
});
Node code:: using request NOTE: this code is certainly wrong since it taking data as json instead of mulitpart/mixed which I dont know and hence the question
....
....
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);
Here's the multipart/form-data that I get currently
------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--
The difference between the different multipart/*
content types is just semantics, so just modify the Content-Type
in req.headers
before sending the request to the REST endpoint:
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);