Elixir POST file to Heroku file Attachment Scanner add-on

192 views Asked by At

I am trying to scan uploaded documents for viruses when a user uploads, using the Heroku Add-on Attachment Scanner.

I am attempting to encode the file directly with Poison.encode, but it is throwing an error so am not sure this is the correct method. Any help appreciated, below is my attempted HTTPoison post request, and the error from 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) error:

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. I need to send the file directly, and am unable to host the image publicly, so the node.js examples in the docs will not work.

2

There are 2 answers

1
Dinesh Balasubramanian On BEST ANSWER
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"] )

will this help you?.. reference

0
JMurphyWeb On

Following on from Dinesh' answer, here is the code snippet which I went for:

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)