Post a base64 image to Imgur

http.get('http://path/to/image.jpg', function (res) {
  var img = '';
  res.on('data', function (buff) {
    img += buff;
  });
  res.on('end', function () {
    var data = querystring.stringify({
      image: img.toString('base64'),
      type: 'base64'
    });
    var opts = {
      host: 'api.imgur.com',
      path: '/3/image',
      method: 'POST',
      headers: {
        'Authorization': 'Client-ID myId',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': data.length
      }
    };
    var req = https.request(opts, function (res) {
      res.on('data', function (buff) {
        console.log(buff.toString());
      });
    });
    req.end(data);
  });
});

img is a string I downloaded from an URL. When I execute it I'm getting:

querystring.js:114
  return encodeURIComponent(str);
         ^
URIError: URI malformed

How do I submit the post data to Imgur properly?

Here's the thing: Your img will be a string with binary data, and it will not be correctly base64-encoded. As switewvu24 mentions, you want to have a buffer when you base64-encode. Unfortunately, that solution isn't quite correct, either.

The least change from what you already have, would be to make a new buffer like this:

var data = querystring.stringify({
  image: new Buffer(img, 'binary').toString('base64')
});

Note the 'binary', which will ensure you get the correct data conversion. Another solution would be to, when you download the image, instead of concatenating a string, collect all the chunks in an array instead, and then make a buffer from that with Buffer.concat:

http.get('http://path/to/image.jpg', function (res) {
  var imgData = [];
  res.on('data', function (buff) {
    imgData.push(buff);
  });
  res.on('end', function () {
    var img = Buffer.concat(imgData);
    var data = querystring.stringify({
      image: img.toString('base64'),
      type: 'base64'
    });
    // ...

Try

var data = querystring.stringify({
  image: new Buffer(img).toString('base64')
});