0

So I want to upload an image to the Tumblr API with the npm request module. Being familiar with the facebook and twitter API, those requests worked like this:

for Facebook:
- create a readstream: var media = fs.createReadStrem('imgpath');
- make a post request with access data and uri set and:

options.formData = {
  source: media,
  caption: "test"
};

for Twitter:
- create readstream
- upload image to twitter:

options.formData = {
  media: media
};

this works flawlessly.
But now with Tumblr, I need to encode the image as "Array (URL-encoded binary contents)" first.

So my question is. How do I encode it and bring it in the right format for the npm request module. To do this, I first need to load the image with fs.readFileSync, but is it possible to upload images to Tumblr as readStream like I did with FB and Twitter?

Here is one of the things I tried:

var img = fs.readFileSync('img');  
options.form = { 
  type: 'photo',
  data: [img.toString('binary')]
}  

which gives me a 400: Error uploading photo.

I also looked into tumblrwks, which works, but I really want to get this done with request

Thank you! :)

Preview
  • 35,317
  • 10
  • 92
  • 112
instance
  • 88
  • 1
  • 4

1 Answers1

0

if you are restricted to use request than you can convert data to hex using the following code

encodeToHex = require('./encode-image').encodeToHex;
var photo = fs.readFileSync('./test/img/P1010486.jpg');

options.form = { 
  type: 'photo',
  data: encodeToHex(photo),
}  

encode-image is used by tumblrwks iteself

KlwntSingh
  • 1,084
  • 8
  • 26
  • The encodeToHex function from encode-image basically does this: var encodeToHex = function(buffer) { return "data:" + (buffer.toString('binary')); }; which I also tried, but tumblr still responds with the same Error. – instance Jul 14 '15 at 12:03
  • @instance if you see code for tumblrwks nodejs in /lib/tumblrwks.js file it is appending image file to oa object and to this object certain authentication parameters are added. – KlwntSingh Jul 14 '15 at 13:19