contenty

Types

A Content-Type header has the general form:

type/subtype; key1=value1; key2=value2
  • The part before the first / is the type (e.g. "text", "application").
  • The part after the / is the subtype (e.g. "html", "json").
  • Optional parameters follow, separated by ; (e.g. "charset=utf-8").

Behavior notes

  • MIME type and subtype are lowercased for consistency
  • Duplicate parameters: the last occurrence wins
  • Quoted parameter values (e.g. profile="...") are returned verbatim
pub opaque type ContentType
pub type ContentTypeError {
  InvalidContentType
}

Constructors

  • InvalidContentType

Values

pub fn mime(content_type: ContentType) -> String

Returns the full MIME string, e.g. "text/html".

pub fn mime_subtype(content_type: ContentType) -> String

Returns the MIME subtype (the part after the slash), e.g. "html".

pub fn mime_type(content_type: ContentType) -> String

Returns the MIME type (the part before the slash), e.g. "text".

pub fn new(
  mime_type: String,
  mime_subtype: String,
  params: dict.Dict(String, String),
) -> Result(ContentType, ContentTypeError)

Creates a new ContentType value from its components.

This function normalises and validates the input:

  • mime_type and mime_subtype are lowercased and trimmed
  • params keys are lowercased and trimmed
  • params values are trimmed (but not lowercased)

Returns Error(ContentTypeError) if either the type or subtype is empty or malformed.

Examples

import contenty
import gleam/dict

let assert Ok(content_type) = contenty.new(
  "Text",
  "HTML",
  dict.from_list([#("Charset", "utf-8")]),
)

contenty.mime(content_type)
// "text/html"

contenty.params(content_type)
// dict.from_list([#("charset", "utf-8")])
pub fn param(
  content_type: ContentType,
  key: String,
) -> Result(String, Nil)

Returns the value of a parameter by key (case-insensitive).

pub fn params(
  content_type: ContentType,
) -> dict.Dict(String, String)

Returns all parameters from the content type.

pub fn parse(
  content_type: String,
) -> Result(ContentType, ContentTypeError)

Parses an HTTP Content-Type header into a structured ContentType value.

Parameters are:

  • Lowercased for keys (charset, boundary, etc.)
  • Trimmed of extra whitespace
  • Parsed safely — malformed entries like "foo" (without =) are ignored

Examples

import contenty

let content_type_1 = contenty.parse("text/html; charset=utf-8")
// Ok(ContentType("text", "html", dict.from_list([#("charset", "utf-8")])))

let content_type_2 = contenty.parse("application/json")
// Ok(ContentType("application", "json", dict.new()))

let content_type_3 = contenty.parse("")
// Error(InvalidContentType)

See also

pub fn to_string(content_type: ContentType) -> String

Converts a ContentType back to its string form.

Produces a canonicalised header string in the form:

type/subtype; key=value; key2=value2

Parameters are joined with ; and appear in insertion order.

Examples

import contenty
import gleam/dict

let assert Ok(content_type) = contenty.new(
  "text",
  "html",
  dict.from_list([#("charset", "utf-8")]),
)

contenty.to_string(content_type)
// "text/html; charset=utf-8"
Search Document