如何使用 twit 通过媒体 POST 请求执行更新? [英] How to perform an update with media POST request using twit?

查看:24
本文介绍了如何使用 twit 通过媒体 POST 请求执行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using twit. Update status(without media) works fine, but update with media is not working.
This is my code (used with 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);
    }
  );
});

我收到的错误是网址参数缺失或无效".
我应该如何通过 media[] 发送图像文件?

The error I am getting is 'Missing or invalid url parameter'.
How should I send the image file via media[]?

推荐答案

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

Make sure your form has a enctype="multipart/form-data" and instead of req.body.img try using req.files.img

从media参数中查看T.post想要什么样的输入,可以试试base64

Check what kind of input T.post wants from the media parameter, you could try 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);
    });
});

这篇关于如何使用 twit 通过媒体 POST 请求执行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆