uploading a photo to facebook using https.request and a multipart form

499 views
Skip to first unread message

Udi

unread,
Apr 27, 2011, 4:43:45 PM4/27/11
to nodejs
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();

Udi

unread,
Apr 27, 2011, 6:42:18 PM4/27/11
to nodejs
one addition. i also tried \r\n for all the places that i have \n
below (based on some docs i found) but that did not help.

shaun etherton

unread,
Apr 27, 2011, 8:46:39 PM4/27/11
to nod...@googlegroups.com
On 28/04/11 8:42 AM, Udi wrote:
> one addition. i also tried \r\n for all the places that i have \n
> below (based on some docs i found) but that did not help.

No experience with Facebook, but the first thing i'd suggest is making
sure you can upload a file to a server you control; just to make sure
everything basically works as expected.

cheers,
- shaun

Udi

unread,
Apr 28, 2011, 8:36:26 PM4/28/11
to nodejs
figured it out.
#1 outputBits.push(FS.readFileSync(fname,enc)+'\n'); should actually
just use S.readFileSync(fname) to get it in binary format
#2 i had to stop using the array for both text and binary and instead
of doing a single req.write() do multiple of those where the binary
data is one of them.



On Apr 27, 5:46 pm, shaun etherton <shaun.ether...@gmail.com> wrote:
> On 28/04/11 8:42 AM, Udi wrote:
>
> > one addition. i also tried \r\n for all the places that i have \n
> > below (based on some docs i found) but that did not help.
>
> No experience withFacebook, but the first thing i'd suggest is making
> sure you canuploada file to a server you control; just to make sure
Reply all
Reply to author
Forward
0 new messages