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:
Declare a new data type with no constructors (e.g. data HTML).
If you want to be able to serialize data *into* that
Content-Type, make an instance of it for MimeRender.
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.
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"):}
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
>>> :{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
If an API can respond with NoContent we assume that this will happen
with the status code 204 No Content. If this needs to be overridden,
WithStatus can be used.