Skip to content

Media Types Reference ​

TMediaType (MARS.Core.MediaType) defines constants for the common MIME types used in [Produces] / [Consumes] and content negotiation. Prefer these constants over string literals.

pascal
[GET, Produces(TMediaType.APPLICATION_JSON)]
function GetData: TData;

Constants ​

ConstantValue
TMediaType.TEXT_PLAINtext/plain
TMediaType.TEXT_PLAIN_UTF8text/plain; charset=utf-8
TMediaType.TEXT_XMLtext/xml
TMediaType.TEXT_HTMLtext/html
TMediaType.TEXT_YAMLtext/yaml
TMediaType.TEXT_EVENT_STREAMtext/event-stream
TMediaType.APPLICATION_XMLapplication/xml
TMediaType.APPLICATION_XML_FireDACapplication/xml-firedac
TMediaType.APPLICATION_JSONapplication/json
TMediaType.APPLICATION_JSON_FireDACapplication/json-firedac
TMediaType.APPLICATION_XHTML_XMLapplication/xhtml+xml
TMediaType.APPLICATION_SVG_XMLapplication/svg+xml
TMediaType.APPLICATION_ATOM_XMLapplication/atom+xml
TMediaType.APPLICATION_OCTET_STREAMapplication/octet-stream
TMediaType.APPLICATION_FORM_URLENCODED_TYPEapplication/x-www-form-urlencoded
TMediaType.APPLICATION_YAMLapplication/x-yaml
TMediaType.APPLICATION_PDFapplication/pdf
TMediaType.MULTIPART_FORM_DATAmultipart/form-data
TMediaType.WILDCARD*/*

Charset constants ​

ConstantValue
TMediaType.CHARSET_NAMEcharset
TMediaType.CHARSET_UTF8utf-8
TMediaType.CHARSET_UTF8_DEFcharset=utf-8
TMediaType.CHARSET_UTF16 / …_DEFutf-16 / charset=utf-16
TMediaType.CHARSET_ISO_8859_1 / …_DEFiso-8859-1 / charset=iso-8859-1

Use them to build a content type that declares its encoding, e.g. TMediaType.TEXT_HTML + '; ' + TMediaType.CHARSET_UTF8_DEF. This matters for textual responses: without an explicit charset, the client applies its own default (historically ISO-8859-1) and non-ASCII characters get mangled. MARS declares UTF-8 on the error responses it produces (TEXT_PLAIN_UTF8 is the default content type of EMARSHttpException) and on the textual files served by TFileSystemResource (see HTML & Templates).

Notes ​

  • */* (WILDCARD) matches any type; writers/readers registered for it act as fallbacks (lowest affinity).
  • FireDAC variants (…-firedac) carry datasets in a compact Delphi-native format; plain application/json carries them as an interoperable array of objects. See FireDAC & Datasets.
  • text/event-stream is used by Server-Sent Events.
  • application/x-yaml requires MARS.YAML.ReadersAndWriters to be registered (used by the OpenAPI endpoint).

Parsing and matching ​

TMediaType.Create('application/json;charset=utf-8') parses a header value (type, subtype and parameters such as charset and quality factor q). Matches performs content-type negotiation, honoring wildcards and q priorities — this is what the content-negotiation layer uses to pick a reader/writer against the request's Accept/Content-Type.

Released under the Apache License 2.0.