https://github.com/breakroom/multipart
Constructs a multipart message, such an HTTP form data request or multipart email
https://github.com/breakroom/multipart
elixir http multipart rfc-2046 rfc-7578
Last synced: about 2 months ago
JSON representation
Constructs a multipart message, such an HTTP form data request or multipart email
- Host: GitHub
- URL: https://github.com/breakroom/multipart
- Owner: breakroom
- License: mit
- Created: 2021-04-19T09:44:19.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-05T07:09:01.000Z (11 months ago)
- Last Synced: 2025-04-02T02:23:43.456Z (2 months ago)
- Topics: elixir, http, multipart, rfc-2046, rfc-7578
- Language: Elixir
- Homepage:
- Size: 26.4 KB
- Stars: 52
- Watchers: 0
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Multipart
[](https://hex.pm/packages/multipart)
[](https://hexdocs.pm/multipart/)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`:
```elixir
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](https://github.com/keathley/finch):
```elixir
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("third body
", [{"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`.
```elixir
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)
```