Multipart.Part (Multipart v0.6.0)

View Source

Represents an individual part of a Multipart message.

Summary

Functions

Builds a Part with a binary body.

Builds a Part with a streaming file body.

Builds a form-data Part with an in-memory file body.

Builds a form-data Part with a streaming file body.

Builds a Part with a Stream body.

Builds a form-data Part with a streaming body.

Builds a form-data Part with a text body.

Types

body()

@type body() :: binary() | Enum.t()

headers()

@type headers() :: [{binary(), binary()}]

name()

@type name() :: String.t() | atom()

t()

@type t() :: %Multipart.Part{
  body: body(),
  content_length: pos_integer() | nil,
  headers: []
}

Functions

binary_body(body, headers \\ [])

@spec binary_body(binary(), headers()) :: t()

Builds a Part with a binary body.

Set the content_length of the Part to the length of the binary.

file_body(path, headers \\ [])

@spec file_body(String.t(), headers()) :: t()

Builds a Part with a streaming file body.

Set the content_length of the Part to the size of the file on disk, as inspected with File.stat.

file_content_field(path, content, name, headers \\ [], opts \\ [])

Builds a form-data Part with an in-memory file body.

Takes the following Keyword options in opts:

  • filename: controls the inclusion of the filename="foo" directive in the content-disposition header. Defaults to true, which uses the filename from the path on disk. Pass in a String to override this, or set to false to disable this directive.

  • content_type: controls the inclusion of the content-type header. Defaults to true which will use MIME.from_path/1 to detect the mime type of the file. Pass in a String to override this, or set to false to disable this header.

file_field(path, name, headers \\ [], opts \\ [])

@spec file_field(String.t(), name(), headers(), Keyword.t()) :: t()

Builds a form-data Part with a streaming file body.

Takes the following Keyword options in opts:

  • filename: controls the inclusion of the filename="foo" directive in the content-disposition header. Defaults to true, which uses the filename from the path on disk. Pass in a String to override this, or set to false to disable this directive.

  • content_type: controls the inclusion of the content-type header. Defaults to true which will use MIME.from_path/1 to detect the mime type of the file. Pass in a String to override this, or set to false to disable this header.

stream_body(stream, headers \\ [])

@spec stream_body(Enum.t(), headers()) :: t()

Builds a Part with a Stream body.

Because the length of the Stream cannot be known up front it doesn't define the content_length. This will cause Multipart.content_length/1 to error unless you set the content_length manually in the struct.

stream_field(stream, name, headers \\ [])

@spec stream_field(Enum.t(), name(), headers()) :: t()

Builds a form-data Part with a streaming body.

text_field(body, name, headers \\ [])

@spec text_field(binary(), name(), headers()) :: t()

Builds a form-data Part with a text body.