Elixir POST 文件到 Heroku 文件附件扫描器附加组件

Elixir POST file to Heroku file Attachment Scanner add-on

我正在尝试使用 Heroku 插件 Attachment Scanner 在用户上传时扫描上传的文档是否有病毒。

我正在尝试使用 Poison.encode 直接对文件进行编码,但它会引发错误,所以我不确定这是正确的方法。感谢任何帮助,下面是我尝试的 HTTPoison post 请求,以及来自 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 示例将不起作用。

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

根据 Dinesh 的回答,这里是我使用的代码片段:

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

file_path = Ev2.Lib.MergerAPI.get_timecard_document_path
body = {:multipart, [{:file, file_path}]}
res = HTTPoison.post(url, body, headers)