HTTP.Blob (http_fetch v0.8.0)

Represents a blob of binary data, similar to JavaScript's Blob.

A Blob contains raw data along with metadata about its MIME type and size. This module implements the Browser Fetch API Blob interface for Elixir.

Examples

# Create a blob
blob = HTTP.Blob.new(<<1, 2, 3, 4>>, "application/octet-stream")

# Access properties
HTTP.Blob.size(blob)  # 4
HTTP.Blob.type(blob)  # "application/octet-stream"

# Convert to binary
data = HTTP.Blob.to_binary(blob)

Summary

Functions

Creates a new Blob from binary data with a specified MIME type.

Returns the Blob's size in bytes.

Converts the Blob to a binary, extracting the raw data.

Returns the Blob's MIME type.

Types

t()

@type t() :: %HTTP.Blob{data: binary(), size: non_neg_integer(), type: String.t()}

Functions

new(data, type \\ "application/octet-stream")

@spec new(binary(), String.t()) :: t()

Creates a new Blob from binary data with a specified MIME type.

Parameters

  • data - Binary data to store in the blob
  • type - MIME type string (default: "application/octet-stream")

Examples

iex> blob = HTTP.Blob.new(<<1, 2, 3>>, "image/png")
iex> blob.type
"image/png"
iex> blob.size
3

size(blob)

@spec size(t()) :: non_neg_integer()

Returns the Blob's size in bytes.

Examples

iex> blob = HTTP.Blob.new(<<1, 2, 3, 4, 5>>, "application/octet-stream")
iex> HTTP.Blob.size(blob)
5

to_binary(blob)

@spec to_binary(t()) :: binary()

Converts the Blob to a binary, extracting the raw data.

Examples

iex> blob = HTTP.Blob.new(<<1, 2, 3>>, "application/octet-stream")
iex> HTTP.Blob.to_binary(blob)
<<1, 2, 3>>

type(blob)

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

Returns the Blob's MIME type.

Examples

iex> blob = HTTP.Blob.new(<<>>, "text/plain")
iex> HTTP.Blob.type(blob)
"text/plain"