SamDecrock / node-http-ntlm

Node.js module to authenticate using HTTP NTLM
MIT License
192 stars 89 forks source link

Corrupts Excel Download #31

Closed ybitso closed 8 years ago

ybitso commented 8 years ago

Hi,

i use httpntlm fine with CSV files, but attempting to download xlsx, it downloads more bytes than it suppose to; appears to be encoding the data differently. Any ideas?

Thanks, A

ybitso commented 8 years ago

Anything ?

SamDecrock commented 8 years ago

Once you have logged in, you should store the cookies and user other modules, like httpreq or request to download the xlsx.

httpntlm is build for authentication.

ybitso commented 8 years ago

I can access the file, it downloads, but seems to download more data than it should for the excel; csv are fine

var url = 'https:\\......\file.xlsx';
var localpath = 'd:\'
var localfile = 'savedfilename.xlsx';
var file = fs.createWriteStream(localpath + localfile);

httpntlm.get({
    url: url,
    username: 'myusername',
    password: 'mypassword',
    workstation: 'myworkstation',
    domain: '',
}, function (err, res){
    if(err) {
        console.error(err);
    }
    else {
        file.write(res.body);
        file.end(function () {
            console.log('Done');
            process.exit()
        })
    }
});
SamDecrock commented 8 years ago

Yes, I know. That's because it converts all responses to an UTF8 String. res.body is a String, not binary data. That's why it works with a CSV (which is pure text and so converting it to string doesn't mess it up).

If you really want to download binary data, use the binary option as described here: https://github.com/SamDecrock/node-httpreq

httpntlm.get({
    url: url,
    username: 'myusername',
    password: 'mypassword',
    workstation: 'myworkstation',
    domain: '',
    binary: true
}, function (err, res){
    if(err) {
        console.error(err);
    }
    else {
        file.write(res.body);
        file.end(function () {
            console.log('Done');
            process.exit()
        })
    }
});

Let me know if it works
ybitso commented 8 years ago

That worked, Thank you