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

This module provides facilities for adding headers to a response.

Example1 expression
let headerVal = addHeader "some-url" 5 :: Headers '[Header "Location" String] Int

The value is added to the header specified by the type (Location in the example above).

  • 3 types
  • 5 classes
  • 5 values
  • Packageservant-0.20.2
  • Exports14
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceResponseHeaders.hs
datadata Headers (ls :: [Type]) a
#

Response Header objects. You should never need to construct one directly. Instead, use addOptionalHeader.

Constructors

Instances6AddHeader, Functor, NFData, GetHeaders, HasStatus, StatusOf
datadata ResponseHeader (sym :: Symbol) a
#
Instances4Functor, Eq, Show, NFData
classclass AddHeader (mods :: [Type]) (h :: Symbol) v orig new | mods h v orig -> new, new -> mods, new -> h, new -> v, new -> orig where
#
Instances4AddHeader
valueaddHeader :: AddHeader '[Optional, Strict] h v orig new => v -> orig -> new
#

addHeader adds a header to a response. Note that it changes the type of the value in the following ways:

  1. A simple value is wrapped in "Headers '[hdr]":

Example2 expressions
let example0 = addHeader 5 "hi" :: Headers '[Header "someheader" Int] String;getHeaders example0[("someheader","5")]
  1. A value that already has a header has its new header *prepended* to the existing list:

Example3 expressions
let example1 = addHeader 5 "hi" :: Headers '[Header "someheader" Int] String;let example2 = addHeader True example1 :: Headers '[Header "1st" Bool, Header "someheader" Int] StringgetHeaders example2[("1st","true"),("someheader","5")]

Note that while in your handlers type annotations are not required, since the type can be inferred from the API type, in other cases you may find yourself needing to add annotations.

valuenoHeader :: AddHeader '[Optional, Strict] h v orig new => orig -> new
#

Deliberately do not add a header to a value.

Example2 expressions
let example1 = noHeader "hi" :: Headers '[Header "someheader" Int] StringgetHeaders example1[]
valuelookupResponseHeader
  1. :: HasResponseHeader h a headers
  2. => Headers headers r
  3. -> ResponseHeader h a
#

Look up a specific ResponseHeader, without having to know what position it is in the HList.

Example3 expressions
let example1 = addHeader 5 "hi" :: Headers '[Header "someheader" Int] Stringlet example2 = addHeader True example1 :: Headers '[Header "1st" Bool, Header "someheader" Int] StringlookupResponseHeader example2 :: ResponseHeader "someheader" IntHeader 5
Example1 expression
lookupResponseHeader example2 :: ResponseHeader "1st" BoolHeader True

Usage of this function relies on an explicit type annotation of the header to be looked up. This can be done with type annotations on the result, or with an explicit type application. In this example, the type of header value is determined by the type-inference, we only specify the name of the header:

Example2 expressions
:set -XTypeApplicationscase lookupResponseHeader @"1st" example2 of { Header b -> b ; _ -> False }True