如何使用 file.upload 和 requests 将文件上传到 slack [英] How to upload files to slack using file.upload and requests

查看:19
本文介绍了如何使用 file.upload 和 requests 将文件上传到 slack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多,但没有找到我正在寻找的答案.

I've been searching a lot and I haven't found an answer to what I'm looking for.

我正在尝试使用 python 请求从 /tmp 上传文件到 slack,但我一直收到 {"ok":false,"error":"no_file_data"}返回.

I'm trying to upload a file from /tmp to slack using python requests but I keep getting {"ok":false,"error":"no_file_data"} returned.

file={'file':('/tmp/myfile.pdf', open('/tmp/myfile.pdf', 'rb'), 'pdf')}
payload={
        "filename":"myfile.pdf", 
        "token":token, 
        "channels":['#random'], 
        "media":file
        }

r=requests.post("https://slack.com/api/files.upload", params=payload)

主要是尝试遵循发布的建议 这里

Mostly trying to follow the advice posted here

推荐答案

通过 http 发送文件比发送其他数据需要更多的工作.您必须设置内容类型并获取文件等等,因此您不能仅将其包含在请求的 payload 参数中.

Sending files through http requires a bit more extra work than sending other data. You have to set content type and fetch the file and all that, so you can't just include it in the payload parameter in requests.

您必须将您的文件信息提供给 .post 方法的 files 参数,以便它可以将所有文件传输信息添加到请求中.

You have to give your file information to the files parameter of the .post method so that it can add all the file transfer information to the request.

my_file = {
  'file' : ('/tmp/myfile.pdf', open('/tmp/myfile.pdf', 'rb'), 'pdf')
}

payload={
  "filename":"myfile.pdf", 
  "token":token, 
  "channels":['#random'], 
}

r = requests.post("https://slack.com/api/files.upload", params=payload, files=my_file)

这篇关于如何使用 file.upload 和 requests 将文件上传到 slack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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