ijunaidfarooq

ijunaidfarooq

How to make a multipart HTTP request using finch

I have a file, such as junaid.jpeg.

I am reading it as data = File.read!("junaid.jpeg") and I want to send that data as multipart data request using finch.

this is what my whole module looks like

defmodule EvercamFinch do

  def request(method, url, headers \\ [], body \\ nil, opts \\ []) do
    transformed_headers = tranform_headers(headers)
    request = Finch.build(method, url, transformed_headers, body)

    case Finch.request(request, __MODULE__, opts) do
      {:ok, %Finch.Response{} = response} ->
        {:ok, response}

      {:error, reason} ->
        {:error, reason}
    end
  end

  defp tranform_headers([]), do: []
  defp tranform_headers(headers) do
    headers
    |> Enum.map(fn({k, v}) ->
      {Atom.to_string(k), v}
    end)
  end
end

and when I send this data, I am getting this error.

iex(5)> EvercamFinch.request(:post, "http://localhost:8888/test/junaid.png", ["Content-Type": "multipart/form-data"], data, [receive_timeout: 15_000])
{:ok,
 %Finch.Response{
   body: "{\"error\":\"no multipart boundary param in Content-Type\"}",
   headers: [
     {"content-type", "application/json"},
     {"server", "SeaweedFS Filer 30GB 2.05"},
     {"date", "Mon, 14 Dec 2020 17:47:28 GMT"},
     {"content-length", "55"},
     {"connection", "close"}
   ],
   status: 500
 }}

I am confused about how I can make a multipart request using finch(mint), is it supported or not? or how to make it work?

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement