EDA.HTTP.Multipart (EDA v0.3.0)

Copy Markdown View Source

Encodes multipart/form-data requests for Discord file uploads.

Produces iodata bodies with a payload_json part and indexed files[n] parts. Automatically injects the attachments array into the JSON payload.

Summary

Functions

Encodes a JSON payload and files into multipart/form-data.

Escapes a filename for use in Content-Disposition headers.

Returns the MIME type for a filename based on its extension.

Functions

encode(json_payload, files)

@spec encode(map(), [EDA.File.t()]) :: {iodata(), String.t()}

Encodes a JSON payload and files into multipart/form-data.

Returns {body_iodata, content_type} where content_type includes the boundary.

The attachments array is automatically injected into the JSON payload with id matching the file index, filename, and optional description.

escape_filename(name)

@spec escape_filename(String.t()) :: String.t()

Escapes a filename for use in Content-Disposition headers.

Escapes backslashes, double quotes, and replaces newlines with underscores.

mime_type(filename)

@spec mime_type(String.t()) :: String.t()

Returns the MIME type for a filename based on its extension.

Falls back to application/octet-stream for unknown extensions.