2

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 !

1 Answer 1

1

found out the mistake,

setting header and replaced

 fs.readFile(files.filetoupload.path, function(err, data) {

with readStream = fs.createReadStream("path to file");

rectified code :

input.x_file_content = readStream;

..

var FormData = require('form-data');
            form_Data = new FormData();        
            form_Data.append('file', request.x_file_content);//No I18N           
            req_body = form_Data;            
        api_headers = form_Data.getHeaders(); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.