HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleservant-0.20.2Haskell2010

Servant.API.ContentTypes

A collection of basic Content-Types (also known as Internet Media Types, or MIME types). Additionally, this module provides classes that encapsulate how to serialize or deserialize values to or from a particular Content-Type.

Content-Types are used in ReqBody and the method combinators:

Example1 expression
type MyEndpoint = ReqBody '[JSON, PlainText] Book :> Put '[JSON, PlainText] Book

Meaning the endpoint accepts requests of Content-Type application/json or text/plain;charset-utf8, and returns data in either one of those formats (depending on the Accept header).

If you would like to support Content-Types beyond those provided here, then:

  1. Declare a new data type with no constructors (e.g. data HTML).

  2. Make an instance of it for Accept.

  3. If you want to be able to serialize data *into* that Content-Type, make an instance of it for MimeRender.

  4. If you want to be able to deserialize data *from* that Content-Type, make an instance of it for MimeUnrender.

Note that roles are reversed in servant-server and servant-client: to be able to serve (or even typecheck) a Get '[JSON, XML] MyData, you'll need to have the appropriate MimeRender instances in scope, whereas to query that endpoint with servant-client, you'll need a MimeUnrender instance in scope.

  • 6 types
  • 8 classes
  • 2 values
  • Packageservant-0.20.2
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceContentTypes.hs

Provided Content-Types

4 declarations
datadata JSON
#
Instances5Accept, MimeRender, MimeUnrender
datadata PlainText
#
Instances9Accept, MimeRender, MimeUnrender, …
datadata FormUrlEncoded
#
Instances5Accept, MimeRender, MimeUnrender
datadata OctetStream
#
Instances7Accept, MimeRender, MimeUnrender, …

Building your own Content-Type

3 declarations
classclass Accept (ctype :: k) where
#

Instances of Accept represent mimetypes. They are used for matching against the Accept HTTP header of the request, and for setting the Content-Type header of the response

Example:

Example3 expressions
import Network.HTTP.Media ((//), (/:))data HTML:{instance Accept HTML where   contentType _ = "text" // "html" /: ("charset", "utf-8"):}
Instances4Accept
  • Accept FormUrlEncodedDefined in servant-0.20.2 · Servant.API.ContentTypes
    application/x-www-form-urlencoded
  • Accept JSONDefined in servant-0.20.2 · Servant.API.ContentTypes
    application/json
  • Accept OctetStreamDefined in servant-0.20.2 · Servant.API.ContentTypes
    application/octet-stream
  • Accept PlainTextDefined in servant-0.20.2 · Servant.API.ContentTypes
    text/plain;charset=utf-8
classclass Accept ctype => MimeRender (ctype :: k) a where
#

Instantiate this class to register a way of serializing a type based on the Accept header.

Example:

data MyContentType

instance Accept MyContentType where
   contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")

instance Show a => MimeRender MyContentType a where
   mimeRender _ val = pack ("This is MINE! " ++ show val)

type MyAPI = "path" :> Get '[MyContentType] Int

Methods

Instances11MimeRender, …
classclass Accept ctype => MimeUnrender (ctype :: k) a where
#

Instantiate this class to register a way of deserializing a type based on the request's Content-Type header.

Example3 expressions
import Network.HTTP.Media hiding (Accept)import qualified Data.ByteString.Lazy.Char8 as BSCdata MyContentType = MyContentType String
Example1 expression
:{instance Accept MyContentType where   contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8"):}
Example1 expression
:{instance Read a => MimeUnrender MyContentType a where   mimeUnrender _ bs = case BSC.take 12 bs of     "MyContentType" -> return . read . BSC.unpack $ BSC.drop 12 bs     _ -> Left "didn't start with the magic incantation":}
Example1 expression
type MyAPI = "path" :> ReqBody '[MyContentType] Int :> Get '[JSON] Int

Methods

Instances11MimeUnrender, …

NoContent

1 declaration
datadata NoContent
#

A type for responses without content-body.

Instances10Eq, Read, Show, Generic, NFData, HasStatus, …

Internal

8 declarations
newtypenewtype AcceptHeader
#
Instances5Eq, Read, Show, Generic, Rep
classclass AllMime list => AllMimeRender (list :: [Type]) a where
#

Methods

Instances4AllMimeRender