项目作者: breakroom

项目描述 :
Constructs a multipart message, such an HTTP form data request or multipart email
高级语言: Elixir
项目地址: git://github.com/breakroom/multipart.git
创建时间: 2021-04-19T09:44:19Z
项目社区:https://github.com/breakroom/multipart

开源协议:MIT License

下载


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:

  1. def deps do
  2. [
  3. {:multipart, "~> 0.1.0"}
  4. ]
  5. 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:

  1. multipart =
  2. Multipart.new()
  3. |> Multipart.add_part(Part.binary_body("first body"))
  4. |> Multipart.add_part(Part.binary_body("second body", [{"content-type", "text/plain"}]))
  5. |> Multipart.add_part(Part.binary_body("<p>third body</p>", [{"content-type", "text/html"}]))
  6. body_stream = Multipart.body_stream(multipart)
  7. content_length = Multipart.content_length(multipart)
  8. content_type = Multipart.content_type(multipart, "multipart/mixed")
  9. headers = [{"Content-Type", content_type}, {"Content-Length", to_string(content_length)}]
  10. Finch.build("POST", "https://example.org/", headers, {:stream, body_stream})
  11. |> Finch.request(MyFinch)

You can construct a multipart/form-data request using the field helpers in Path.

  1. multipart =
  2. Multipart.new()
  3. |> Multipart.add_part(Part.text_field("field 1 text", :field1))
  4. |> Multipart.add_part(Part.file_field("/tmp/upload.jpg", :image))
  5. body_stream = Multipart.body_stream(multipart)
  6. content_length = Multipart.content_length(multipart)
  7. content_type = Multipart.content_type(multipart, "multipart/form-data")
  8. headers = [{"Content-Type", content_type}, {"Content-Length", to_string(content_length)}]
  9. Finch.build("POST", "https://example.org/", headers, {:stream, body_stream})
  10. |> Finch.request(MyFinch)