node-js-libs/curlrequest

Issue with encoding = null

CoinCoderBuffalo opened this issue · 2 comments

I am attempting to connect to an external server over HTTPS with basic authentication to download an image in binary format. When setting the enconding to null, I get the error "Failed to initialize". Full code below:

exports.slideImageData = function (req, res){
var id = req.params.id;
var prefix = "data:image/png;base64, ";
var options = {
url: config.jive.domain + 'api/core/v3/people/' + id + '/images/1/data'
, user: config.jive.user + ':' + config.jive.pass
, encoding: null
};
curl.request(options, function (err, result){
if (err){
console.log('error: ' + err)
} else {
var base64Image = new Buffer(result, 'binary').toString('base64');
base64Image = prefix + base64Image;
res.send('');
}
});
}

I also ran into the problem of using "encoding = null". It turns out that the problem starts within the "copy" function within index.js. When the object copier finds a property that's assigned to null, it replaces it with an empty object. Thus, "encoding = null" becomes "encoding = {}"

Here's one possible solution to that problem:

exports.copy = function (obj) {
    var copy = {};
    for (var i in obj) {
        if (typeof obj[i] === 'object') {
            // handle properties that are set to null
            copy[i] = (obj[i] === null) ? null : exports.copy(obj[i]);
        } else {
            copy[i] = obj[i];
        }
    }
    return copy;
};

The next problem is that it now throws a different error: Failed to initialize.

This is because curl is now receiving the command line arguments --encoding null, due to the way the args are processed/composed, and can be fixed like this:

        values.forEach(function (value) {
            // don't send a flag for null values
            if (value !== null) {
                args.push('--' + key);
                if (true !== value) {
                    args.push(value);
                }
            }
        });

This library is exceptionally handy, and I'd love to see it continue to improve!

I've just pushed 0.4.1 which should fix the issue