开发者

How to post an uploaded file with http?

How would I do the equivalent of this in an express app? That is, posting a file to facebook:

curl -F 'access_token=xyz' \
    -F 'source=@file.png' \
    -F 'message=Caption for the photo' \
    https://graph.facebook.com/me/photos

I'm using the following to upload the file from the example in the repo:

app.post('/', function(req, res, next){
 req.form.complete(function(err, fields, files){
开发者_高级运维   if (err) {
     next(err);
   } else {
     console.log('\nuploaded %s to %s'
       ,  files.image.filename
       , files.image.path);
     res.redirect('back');
   }
 });
})


Take a look at the request-module, which makes it (almost) too easy:

fs.readStream('file.png').pipe(request.post('http://graph.facebook.com/me/photos'))

This will create a POST-request to the given URL and stream file.png though it. It should be fairly trivial to add the remainder of your fields.


You can create an HTTP request with Node. See the following example in the docs:

http://nodejs.org/docs/v0.4.0/api/http.html#http.request

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜