Skip to content

Latest commit

 

History

History
67 lines (47 loc) · 2.04 KB

README.md

File metadata and controls

67 lines (47 loc) · 2.04 KB

Multipart

Hex pm Hex Docs

Constructs a multipart message, such an HTTP form data request or multipart email.

Features

  • Follows RFC 2046 and RFC 7578
  • Can stream the request body, reducing memory consumption for large request bodies

Requirements

  • Elixir >= 1.10
  • Erlang/OTP >= 21

Installation

Add multipart to your list of dependencies in mix.exs:

def deps do
  [
    {:multipart, "~> 0.1.0"}
  ]
end

Usage

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", to_string(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", to_string(content_length)}]

Finch.build("POST", "https://example.org/", headers, {:stream, body_stream})
|> Finch.request(MyFinch)