Constructs a multipart message, such an HTTP form data request or multipart email.
- Follows RFC 2046 and RFC 7578
- Can stream the request body, reducing memory consumption for large request bodies
- Elixir >= 1.9
- Erlang/OTP >= 21
Add multipart
to your list of dependencies in mix.exs
:
def deps do
[
{:multipart, "~> 0.1.0"}
]
end
Typically, you'll use Multipart
to construct the HTTP body and headers, and use those to build a request in an HTTP client such as Finch:
multipart =
Multipart.new()
|> Multipart.add_part(Part.binary_body("first body"))
|> Multipart.add_part(Part.binary_body("second body", [{"content-type", "text/plain"}]))
|> Multipart.add_part(Part.binary_body("<p>third body</p>", [{"content-type", "text/html"}]))
body_stream = Multipart.body_stream(multipart)
content_length = Multipart.content_length(multipart)
content_type = Multipart.content_type(multipart, "multipart/mixed")
headers = [{"Content-Type", content_type}, {"Content-Length", content_length}]
Finch.build("POST", "https://example.org/", headers, {:stream, body_stream})
|> Finch.request(MyFinch)
You can construct a multipart/form-data
request using the field helpers in Path
.
multipart =
Multipart.new()
|> Multipart.add_part(Part.text_field("field 1 text", :field1))
|> Multipart.add_part(Part.file_field("/tmp/upload.jpg", :image))
body_stream = Multipart.body_stream(multipart)
content_length = Multipart.content_length(multipart)
content_type = Multipart.content_type(multipart, "multipart/form-data")
headers = [{"Content-Type", content_type}, {"Content-Length", content_length}]
Finch.build("POST", "https://example.org/", headers, {:stream, body_stream})
|> Finch.request(MyFinch)