HTTPoison 上传文件

sge*_*ger 3 elixir

是否有任何教程如何使用 HTTPoison.post 上传本地文件?打开文件

{:ok, file} = File.open "README.md"
HTTPoison.post("#{url}", file, headers)
Run Code Online (Sandbox Code Playgroud)

这是用于使用上传功能的 Dropbox 集成

curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer " \
--header "Dropbox-API-Arg: {\"path\":\"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
Run Code Online (Sandbox Code Playgroud)

问题是如何发送——数据二进制文件。

有任何想法吗?

谢谢

Dog*_*ert 5

我相信curl's的等价物--data-binary{:file, path}用作主体,如Hackney 的 README.md 中所述

这应该发送与您的curl示例相同的请求:

HTTPoison.post("https://content.dropboxapi.com/2/files/upload",
               {:file, "local_file.txt"},
               [{"Authorization", "Bearer "}, ...])
Run Code Online (Sandbox Code Playgroud)


Don*_*ter 5

有些网站需要多部分表单数据。如果上面的示例不适用于您的目标,请尝试此操作。

HTTPoison.post!(
  url,
  {:multipart, [{:file, file}]},
  [],
  recv_timeout: 30000
 )
Run Code Online (Sandbox Code Playgroud)