Skip to content

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

License

Notifications You must be signed in to change notification settings

addvalue-gmbh/multipart

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.9
  • 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", 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)

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 100.0%