1

当用户上传时,我正在尝试使用 Heroku Add-on 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(file) 错误:

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

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

4

2 回答 2

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"] )

这会对你有帮助吗?...参考

于 2018-05-23T16:06:11.600 回答
0

继 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)
于 2018-05-24T07:01:28.820 回答