Elixir POST文件到Heroku文件附件扫描程序附加组件

时间:2018-05-23 14:14:55

标签: heroku elixir elixir-poison httpoison

我正在尝试使用Heroku附加组件Attachment Scanner在用户上传时扫描上传的文档中的病毒。

我正在尝试使用Poison.encode直接对文件进行编码,但是它会抛出一个错误,所以我不确定这是正确的方法。感谢任何帮助,下面是我尝试过的HTTPoison帖子请求,以及来自Poison.encode的错误!。

def scan do
    url = System.get_env("ATTACHMENT_SCANNER_URL") <> "/requests"
    token = System.get_env("ATTACHMENT_SCANNER_API_TOKEN")

    headers =
      [
        "Authorization": "bearer " <> token,
        "Content-Type": "multipart/form-data",
      ]

    file_path = local_path_to_pdf_file
    file = file_path |> File.read!

    body = Poison.encode!(%{file: file})

    res = HTTPoison.post(url, body, headers, recv_timeout: 40_000)
  end

Poison.encode(文件)错误:

iex(3)> Poison.encode(file)
** (FunctionClauseError) no function clause matching in Poison.Encoder.BitString.chunk_size/3

    The following arguments were given to Poison.Encoder.BitString.chunk_size/3:

        # 1
        <<226, 227, 207, 211, 13, 10, 49, 48, 51, 32, 48, 32, 111, 98, 106, 13, 60, 60,
          47, 76, 105, 110, 101, 97, 114, 105, 122, 101, 100, 32, 49, 47, 76, 32, 50,
          53, 50, 53, 51, 52, 51, 47, 79, 32, 49, 48, 53, 47, 69, 32, ...>>

        # 2
        nil

        # 3
        1

PS。我需要直接发送文件,并且无法公开托管图像,因此文档中的node.js示例将无效。

1 个答案:

答案 0 :(得分:0)

file = "/some/path/video.mp4" 
HTTPoison.post( "api.vid.me/video/upload";, {:multipart, [{:file, file, {"form-data", [name: "filedata", filename: Path.basename(file)]}, []}]}, ["AccessToken": "XXXXX"] )

这会对你有帮助吗?.. reference