Post 在 R 中使用 httr 包松弛图像

Post image to slack using httr package in R

Slack 提供了一种通过 api 上传文件的方法。文档可在此处找到:

Slack files.upload method

在此页面上,它给出了如何 post 文件的示例:

curl -F file=@dramacat.gif -F "initial_comment=Shakes the cat" -F channels=C024BE91L,D032AC32T -H "Authorization: Bearer xoxa-xxxxxxxxx-xxxx" https://slack.com/api/files.upload

我正在尝试翻译如何使用 R 中的 httr 包执行这行代码,我的 R 工作目录中有一个文件。我在翻译命令的不同部分时遇到问题。这是我目前所拥有的。

api_token='******'
f_path='c:/mark/consulting/dreamcloud' #this is also my working directory
f_name='alert_picture.png'

res<-httr::POST(url='https://slack.com/api/files.upload', httr::add_headers(`Content-Type` = "multipart/form-data"), 
      body = list(token=api_token, channels='CCJL7TMC7', title='test', file = httr::upload_file(f_path), filename=f_name))

当我 运行 出现以下错误时:

Error in curl::curl_fetch_memory(url, handle = handle) : 
  read function returned funny value

我试图找到更好的示例来使用,但到目前为止没有成功。任何建议表示赞赏!

There's an example in slackr's own gg_slackr method,创建 GGPlot 图像并将其上传到 Slack:

  res <- POST(url="https://slack.com/api/files.upload",
              add_headers(`Content-Type`="multipart/form-data"),
              body=list(file=upload_file(ftmp),
                        token=api_token, channels=modchan))

您的代码似乎将目录路径而不是文件路径作为 file 参数传递 - 考虑将该参数更改为 file=upload_file(paste(f_path, f_name, sep="/") 并查看是否可以修复您的错误。