gpt4 book ai didi

json - 如何使用 twit 通过媒体 POST 请求执行更新?

转载 作者:搜寻专家 更新时间:2023-11-01 00:02:34 27 4
gpt4 key购买 nike

我正在使用 twit .更新状态(无媒体)工作正常,但 update with media不工作。
这是我的代码(与 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);
}
);
});

我收到的错误是“缺少或无效的 url 参数”
我应该如何通过 media[] 发送图像文件?

最佳答案

确保您的表单有 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);
});
});

关于json - 如何使用 twit 通过媒体 POST 请求执行更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com