hubertlepicki
Encoding multipart/form-data HTTP body
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 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 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?
Most Liked
engineeringdept
I needed to construct multipart messages recently, but also didn’t want to include Tesla in my dependencies, so I built a separate library for message construction. It should cover most use cases for multipart/form-data requests, but very open to suggestions to improve the API and functionality.
hubertlepicki
So the intent behind creating streaming one is that I have to send multipart requests that contain files, and these files can be many megabytes in size. If I do N of these requests at the same time, I could easily end up exhausting my memory quota.
So, the intent was not to make it more performant, but make it not crash production server.
I actually have no idea how the streaming performance versus non-streaming compares. It’d be good test to do.
hubertlepicki
I don’t think Finch supports building actual multipart body. This is a task on it’s own.
What I am doing is using Tesla to help me out with that task.
First, build multipart body using Tesla.Multipart
multipart =
Multipart.new()
|> Multipart.add_content_type_param("charset=utf-8")
|> add_fields(form_data)
|> add_files(file_parts)
mp_headers = Multipart.headers(multipart)
stream = Multipart.body(multipart)
headers = ... # your other headers, optional
And then you can either POST it as a streaming body request, once this PR is merged https://github.com/keathley/finch/pull/107
:post
|> Finch.build(url, headers ++ mp_headers, {:stream, stream})
|> Finch.request(YourFinch)
Or, you can POSt it in non-streaming fashion (should work on current release/master):
:post
|> Finch.build(url, headers ++ mp_headers, stream |> Enum.join())
|> Finch.request(YourFinch)
Last Post!
simon
A huge thank you for putting multipart together. I’m using the PSPDFKit API in an application where I was already using Finch and it helped me enormously.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









