# Encoding multipart/form-data HTTP body

**URL:** <https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443>\
**Category:** Discussions\
**Tags:** http\_client, discussion, http, httpoison, hackney, mint\
**Created:** [June 27, 2019, 11:05am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443 "2019-06-27T11:05:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [June 27, 2019, 11:05am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/1 "2019-06-27T11:05:13Z")

</div>

Does anyone here know of an Elixir library which would help me assemble bodies of HTTP requests with “multipart/form-data” Content-type?

This would be the reverse of [plug/lib/plug/parsers/multipart.ex at v1.8.2 · elixir-plug/plug · GitHub](https://github.com/elixir-plug/plug/blob/v1.8.2/lib/plug/parsers/multipart.ex#L39) i.e. parsing “multipart/form-data” requests by Plug.

Ideally I could either assemble it and get it as String, or in streaming form (for large file uploads).

Hackney [does that](https://github.com/benoitc/hackney/blob/master/doc/hackney_multipart.md#encode_form-1) and I can try translating that to Elixir, but would be ideal if I didn’t have to for obvious reasons :D.

Does anyone know of a piece of Elixir code that does that, even if it’s part of bigger library?

---

<div class="post-metadata">

**Author:** ![jola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jola/32/18010_2.png) [@jola](https://forum.elixirforum.com/u/jola)\
**Post date:** [June 27, 2019, 11:24am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/2 "2019-06-27T11:24:09Z")

</div>

HTTPoison wraps hackney so you can avoid translating and supports multipart. Haven’t used it for this myself.

[https://github.com/edgurgel/httpoison#multipart](https://github.com/edgurgel/httpoison#multipart)

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [June 27, 2019, 11:31am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/3 "2019-06-27T11:31:06Z")

</div>

Thanks. A bit of a background I should have posted: I want to avoid using HTTPoison / hackney and use either [mint](https://github.com/ericmj/mint) directly or with a wrapper called [mojito](https://github.com/appcues/mojito). Unfortunately these don’t have part that assembles “multipart/form-data” requests, so I either have to find one from some other library, or write one myself.

---

<div class="post-metadata">

**Author:** ![jola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jola/32/18010_2.png) [@jola](https://forum.elixirforum.com/u/jola)\
**Post date:** [June 27, 2019, 11:47am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/4 "2019-06-27T11:47:27Z")

</div>

Ah, I see. Yeah, since they’re pretty new and Mint itself is not necessarily designed for “regular” use cases (although mojito is), you’re likely to have to write something yourself if mojito doesn’t support it.

Out of curiosity, why does it have to be mint?

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [June 27, 2019, 11:58am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/5 "2019-06-27T11:58:08Z")

</div>

Hackney has got [issues](https://github.com/timberio/timber-elixir/issues/346) that I stumbled upon recently (among others before), but I don’t think I like how it depends on application configuration either, things get quite difficult once you have to deal with SSL / self-signed certificates or client SSL authentication. And I had fair amount of issues on production with using it too, although not recent. There’s a [thread](https://forum.elixirforum.com/t/http-client-libraries-need-better-one-in-standard-library/4357/20) on this forum from 2017 where I argued we need a better built-in HTTP client library in Elixir itself.

Mint comes as close as it gets to what I wanted the standard HTTP client to be, it’s idiomatic Elixir, it’s low level enough that you can build idiomatic clients on top of it (like mojito) and from what I saw really like it’s design. It’s a good reading if one wants to learn how to deal with TCP/SSL connections in Elixir too. Very educational 🙂

I think I prefer to put in some effort working on getting it up to desired shape myself rather than plan for another battle agains HTTPoison/hackney.

---

<div class="post-metadata">

**Author:** ![jola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jola/32/18010_2.png) [@jola](https://forum.elixirforum.com/u/jola)\
**Post date:** [June 27, 2019, 12:03pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/6 "2019-06-27T12:03:14Z")

</div>

Got it, that makes sense 🙂 I’ve had some obscure problems with HTTPoison (hackney) too, although nothing I couldn’t just ignore…

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [June 27, 2019, 12:32pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/7 "2019-06-27T12:32:15Z")

</div>

`tesla` can do MultiPart on all of its backends as far as I know… You are free too choose between `hackeny`, `httpc` and `ibrowse` IIRC…

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [June 27, 2019, 12:36pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/8 "2019-06-27T12:36:55Z")

</div>

@NobbZ ah, great catch! It’s there indeed: [tesla/lib/tesla/multipart.ex at master · elixir-tesla/tesla · GitHub](https://github.com/teamon/tesla/blob/master/lib/tesla/multipart.ex)

I’ll have to think on how to extract this possibly to shared lib but for now I can just include tesla as a proof of concept.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [June 27, 2019, 12:38pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/9 "2019-06-27T12:38:29Z")

</div>

Ah, so you do not actually want to send the request, you just want to have the encoded body?

Then I missunderstood your intial question, but I’m happy that I still was able to help 😃

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [June 27, 2019, 12:41pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/10 "2019-06-27T12:41:22Z")

</div>

> [@NobbZ](#):
>
> Then I missunderstood your intial question, but I’m happy that I still was able to help 😃

You did but that’s helpful anyway, I was counting on such example code but didn’t think about Tesla.

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 13, 2020, 11:10am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/11 "2020-12-13T11:10:29Z")

</div>

does Finch support multipart data request?

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [December 13, 2020, 11:31am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/12 "2020-12-13T11:31:31Z")

</div>

There is an open PR to support it.

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 13, 2020, 12:51pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/13 "2020-12-13T12:51:25Z")

</div>

when they will merge? is there time span on that?

I am going to switch from HTTPoison to Finch, and the whole application based on sending multipart requests.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [December 13, 2020, 1:04pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/14 "2020-12-13T13:04:18Z")

</div>

I think they will but need some minor tweaks in terms of naming things I thunk. I will try to do it tonight and let you know. Nothing from API should change so I guess you can try from my branch/pr, in fact, another pair of eyes testing this would be very much welcome.

niedz., 13 gru 2020, 14:01 użytkownik Ijunaidfarooq via Elixir Programming Language Forum \<[noreply@forum.elixirforum.com](mailto:noreply@forum.elixirforum.com)\> napisał:

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 13, 2020, 1:11pm UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/15 "2020-12-13T13:11:02Z")

</div>

Sure thank you.

---

<div class="post-metadata">

**Author:** ![achempion](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achempion/32/21179_2.png) [@achempion](https://forum.elixirforum.com/u/achempion)\
**Post date:** [December 14, 2020, 3:04am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/17 "2020-12-14T03:04:35Z")

</div>

> [@hubertlepicki](#):
>
> I want to avoid using HTTPoison / hackney

It seems like they can solve your issue. Why do you think using them wouldn’t fit into your project?

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [December 14, 2020, 10:10am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/18 "2020-12-14T10:10:07Z")

</div>

I could use hackney directly, yes, and I have been doing it but it’s a low level API. As far as I know, and I inspected HTTPoison code pretty intensely to find a way to do it, it can’t stream _request body_. It can stream the response from the server, but it doesn’t allow me to stream request. I think hackney or ibrowse (or a Tesla wrapper for either) used directly could do it, however.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [December 14, 2020, 10:11am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/19 "2020-12-14T10:11:37Z")

</div>

And I think you may have asked here about documentation and you are right, once my PR is merged I will work on some documentation to how to use it. I suppose you figured out the way to use http basic auth but otherwise hit me up, I am doing the same.

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 14, 2020, 10:40am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/20 "2020-12-14T10:40:57Z")

</div>

I did this way

```
credentials = "#{username}:#{password}" |> Base.encode64()
EvercamFinch.request(:get, url, [Authorization: "Basic #{credentials}"])

```

and module is

```elixir
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

```

are you doing any different?

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 15, 2020, 5:20am UTC](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443/21 "2020-12-15T05:20:03Z")

</div>

Hi, I am little confused about the PR you pointed me out, on Finch.

my use case is, I am trying to send a multipart request using Finch.

in past with HTTPoison. it was like this

```
HTTPoison.post(
  url,
  {:multipart, [{file_path, image, []}]},
  [],
  hackney: [pool: :seaweedfs_upload_pool, recv_timeout: 15_000]
)

```

URL = [http://localhost:8888/everc-dupzo/snapshots/oldest-1572566369.jpg](http://localhost:8888/everc-dupzo/snapshots/oldest-1572566369.jpg)  
file\_path = /snapshots/oldest-1572566369.jpg  
and the image is a binary data, which you can get while doing File.read!.

I have done every other replacement of HTTPoison but this has become a road blocker for me.

I have also tried to do simple post request using the data but it’s not working either.

Can you guide me how I Can send an Image binary data as a multipart request?

NOTE: in above HTTPoison request, the image is not saved on disk at all.

[Next page](https://forum.elixirforum.com/t/encoding-multipart-form-data-http-body/23443.md?page=2)
