如何使用twit进行媒体POST请求更新?

3

我正在使用twit。更新状态(不带媒体)正常工作,但更新媒体无法工作。
这是我的代码(与express一起使用):

//client side

<form id="tweeter" action='/image' method='POST' >
  <input type="text" name="tw" id="tw" />
  <input type='file' name='img' id='img' /> 
  <input type="submit" value="submit" /> 
</form>


 //server side

app.post('/image',function(req,res){
  var f= "./" +req.body.img;
  console.log(req.body.img);
  T.post('statuses/update_with_media', 
    { status: req.body.tw, media: f }, 
    function(err, reply) {
      console.log('ERROR:' +err);
      console.log('REPLY:' +reply);
    }
  );
});

我收到的错误是'Missing or invalid url parameter'。
我应该如何通过media[]发送图片文件?
1个回答

1

确保你的表单有一个enctype="multipart/form-data",而不是使用req.body.img,尝试使用req.files.img

检查T.post从媒体参数中需要什么类型的输入,你可以尝试使用base64

客户端代码示例:

<form id="tweeter" enctype="multipart/form-data" action='/image' method='POST' >
   <input type="text" name="tw" id="tw" />
   <input type='file' name='img' id='img' /> 
   <input type="submit" value="submit" /> 
</form>

示例服务器代码:

app.post('/image',function(req,res){
    var f = fs.readFileSync(req.files.img.path,'base64');
    T.post('statuses/update_with_media', {status: req.body.tw, media:f}, function(err, reply) {
        console.log('ERROR:'+err);
        console.log('REPLY:'+reply);
    });
});

我包含了fs库,并尝试了上述代码。它报错,提示“数据无效”。 - Aravind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接