i am trying to use https.request to upload a photo to facebook. the
call fails on a facebook "unknown error". although this is a facebook
related issue i was hoping someone here had encountered (and solved)
this.
below is the code. a few notes:
- using binary encoding facebook complains that the image file is
invalid, for base64 i get the "unknown error" failure
- i tried using expect header with no luck
- all facebook permissions/keys/etc are ok. this works with command
line curl.
var FS = require('fs');
var https = require('https');
var enc = 'base64'
var fname = 'image.jpg'
var authKey= 'SECRET';
var outputBits = [];
outputBits.push('------------0xKhTmLbOuNdArY\n');
outputBits.push('Content-Disposition: form-data; name="access_token"\n
\n');
outputBits.push(authKey+'\n');
outputBits.push('------------0xKhTmLbOuNdArY\n');
outputBits.push('Content-Disposition: form-data; name="message"\n\n');
outputBits.push('my caption\n');
outputBits.push('------------0xKhTmLbOuNdArY\n');
outputBits.push('Content-Disposition: form-data; name="source";
filename="'+fname+'"\n');
outputBits.push('Content-Type: image/jpeg\n');
outputBits.push('Content-Transfer-Encoding: '+enc+'\n\n');
outputBits.push(FS.readFileSync(fname,enc)+'\n');
outputBits.push('------------0xKhTmLbOuNdArY--\n');
var output = outputBits.join("");
var options = {
host: '
graph.facebook.com',
port: 443,
path: '/me/photos',
method: 'POST',
headers: {'Content-Type': 'multipart/form-data;
boundary=----------0xKhTmLbOuNdArY','Content-Length': output.length}
};
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
// write data to request body
req.write(output);
req.end();