Requirement:
I need to make an api call with the file uploaded by the user.My server is node and I use request module for making api calls. Below is the code when user uploads a file and submits it.
if(queryData.sub == "upload"){
var input = {};
var formidable = require('formidable');
var form = new formidable.IncomingForm();
form.parse(request, function (err, fields, files) {
var fs = require('fs');
fs.readFile(files.filetoupload.path, function(err, data) {
input.x_file_content = data;
client.API.ATTACHMENTS.uploadFile(input).then(function(resp){
var str = settings.layoutParsing(resp);
response.write(str);
response.end();
})
});
});
}
}
In upload file function i use FormData to set the file and send it while making api call .Below is the code:
if (request.x_file_content) {
var FormData = require('form-data');
var formData = new FormData();
formData.append('file', request.x_file_content);//No I18N
req_body = formData;
}
...
var httpclient = require('request');
httpclient({
uri : baseUrl,
method : request.type,
headers : api_headers,
responseType : responseType,
body : req_body
},function(error,response,body){
Problem:
but the file was not successfully sent and multipart content required error is thrown by the api server.
Can anyone point out what mistake Im doing.
Thanks !