node.jsresponsenodejs-streamrequest-response

Node js :- Response chunks to file re-named with content-disposition


I am trying to get a document (pdf/docx/zip/..) from an API and save it with the file name received at content-disposition in response header. I have tried the below methods. But either the file is not saved with the actual file name or the file is saved with corrupted (0kb) file. I have been trying this for 2 days now. Could someone point out where this is going wrong ?

var calloptions = {
    method: 'GET',
    url: 'https://ip:port/address',
    qs: { docId: 'something toffetch' },
    headers:
        {
            'content-type': 'application/json',
            authorization: postData.bcAccessToken
        }
};
console.log("calloptions", calloptions);
var filepath = require('path').join(require('os').homedir(), 'Desktop');
var name = 123;
var myFile = [];
request(calloptions, function (error, response, body) {
    if (error) throw new Error(error);
    var chunks = [];
    name = 456;
    // var myFile ;
    //original=;
    console.log(response.headers["content-disposition"]);
    if (response.headers["content-disposition"]) {
        myFile.push(filepath + "\\" + response.headers["content-disposition"].split("=").pop());
        console.log(myFile);

        //        console.log(myFile);
        console.log("BEFORE WRITTING");
        response.on('data', function (chunk) {
            while (chunk = this.read()) {
                chunks.push(chunk);
                //chunk.pipe(fs.appendFile(filepath+"\\"+response.headers["content-disposition"].split("=").pop()));
            }

            // fs.appendFileSync(myFile, chunk);
            // fs.appendFile(myFile, chunk, function (err) {
            //     if(err) throw err;
            //     console.log("DURING WRITTING");
            //     console.log("myFile :- ",myFile);
            // });
            //chunks.pipe(fs.appendFile(filepath+"\\"+response.headers["content-disposition"].split("=").pop()));
            // console.log("writing completed to file");
        });//.pipe(fs.createWriteStream(filepath+"\\"+response.headers["content-disposition"].split("=").pop()));
        console.log("AFTER WRITTING");
    }
}).on('end', () => {
    console.log('There will be no more data.');
    console.log("IN THE END - ");
    console.log(name);
    console.log(myFile);
});//.pipe(fs.createWriteStream(filepath+"\\"+name));
console.log("AFTER REQUEST");                      

Solution

  • I solved it by using async.waterfall as below. But a better way to solve this is always appreciated.

    async.waterfall([
         function (callback) {
             request(calloptions, function (error, response, body) {
                 if (error) throw new Error(error);
                 console.log(response.headers["content-disposition"]);
                 if (response.headers["content-disposition"]) {
    
                     //var myFile2 = filepath + "\\" + response.headers["content-disposition"].split("=").pop();
                     var body = '';
                     response.on('data', function (chunk) {
                         body += chunk;
                     });
                     callback(null, response.headers["content-disposition"].split("=").pop());
                 }
             }).pipe(fs.createWriteStream(filepath+"\\"+"Orig.pdf"));
         },
         function (res, callback) {
             console.log("SECOND FUNCTION");
             console.log(res);
             fs.rename(filepath+"\\"+"Orig.pdf", filepath+"\\"+res, function (err) {
                 if (err) throw err;
                 console.log('renamed complete');
               });
         }
     ])