ReqMulti (req_multi v0.1.1)

Copy Markdown View Source

A Req plugin for sending multipart/form-data request bodies.

It builds on the multipart library: you assemble a Multipart struct and hand it to Req through the :multi option. The plugin then sets the Content-Type (with the multipart boundary) and Content-Length headers and streams the encoded body.

Usage

multipart =
  Multipart.new()
  |> Multipart.add_part(Multipart.Part.text_field("hello world", "greeting"))

Req.new()
|> ReqMulti.attach()
|> Req.post!(multi: multipart)

Building the multipart

The %Multipart{} struct comes from the multipart library. Start with Multipart.new/0 and add one part per field or attachment with Multipart.add_part/2, using a builder from Multipart.Part:

Multipart.new()
# a plain form field
|> Multipart.add_part(Multipart.Part.text_field("hello world", "greeting"))
# a file read from disk
|> Multipart.add_part(Multipart.Part.file_field("/path/to/photo.png", "photo"))
# a file whose bytes you already hold
|> Multipart.add_part(Multipart.Part.file_content_field("report.pdf", pdf, "document"))

Other common builders are Multipart.Part.stream_field/3 and the lower-level binary_body/2/file_body/2. See the Multipart.Part docs for the full list and their optional headers/opts arguments.

Options

  • :multi - a %Multipart{} struct to encode and send as the request body. When set to any other value, the request raises an ArgumentError. When omitted, the request is sent unchanged.

Summary

Functions

Attaches the plugin to a Req.Request, registering the :multi option.

Functions

attach(req)

@spec attach(req :: Req.Request.t()) :: Req.Request.t()

Attaches the plugin to a Req.Request, registering the :multi option.

Call once when building your request. See the module documentation for the :multi option and a full example.

Examples

iex> Req.new() |> ReqMulti.attach()