简体   繁体   中英

Upload photo to Tumblr API with nodejs and request module

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! :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM