servant-0.2: A family of combinators for defining webservices APIs and serving them

Safe HaskellNone
LanguageHaskell2010

Servant.API.Header

Synopsis

Documentation

data Header sym a Source

Extract the given header's value as a value of type a.

Example:

newtype Referer = Referer Text
  deriving (Eq, Show, FromText, ToText)

           -- GET /view-my-referer
type MyApi = "view-my-referer" :> Header "from" Referer :> Get Referer

Instances

(KnownSymbol sym, FromText a, HasServer sublayout) => HasServer ((:>) * (Header Symbol * sym a) sublayout)

If you use Header in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of the type specified by Header. This lets servant worry about extracting it from the request and turning it into a value of the type you specify.

All it asks is for a FromText instance.

Example:

newtype Referer = Referer Text
  deriving (Eq, Show, FromText, ToText)

           -- GET /view-my-referer
type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get Referer

server :: Server MyApi
server = viewReferer
  where viewReferer :: Referer -> EitherT (Int, String) IO referer
        viewReferer referer = return referer
type Server ((:>) * (Header Symbol * sym a) sublayout) = Maybe a -> Server sublayout