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 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

Search Document