HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulescotty-0.30Haskell2010

Web.Scotty.Cookie

This module provides utilities for adding cookie support inside scotty applications. Most code has been adapted from 'scotty-cookie'.

Example

A simple hit counter that stores the number of page visits in a cookie:

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad
import Data.Monoid
import Data.Maybe
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Read as TL (decimal)
import Web.Scotty (scotty, html, get)
import Web.Scotty.Cookie (getCookie, setSimpleCookie)

main :: IO ()
main = scotty 3000 $
    get "/" $ do
        hits <- liftM (fromMaybe "0") $ getCookie "hits"
        let hits' =
              case TL.decimal $ TL.fromStrict hits of
                Right n -> TL.pack . show . (+1) $ (fst n :: Integer)
                Left _  -> "1"
        setSimpleCookie "hits" $ TL.toStrict hits'
        html $ mconcat [ "<html><body>"
                       , hits'
                       , "</body></html>"
                       ]
  • 3 types
  • 19 values
  • Packagescotty-0.30
  • Exports22
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceCookie.hs

Set cookie

2 declarations

Get cookie(s)

2 declarations

Delete a cookie

1 declaration
valuedeleteCookie
  1. :: MonadIO m
  2. => Text

    name

  3. -> ActionT m ()
#

Browsers don't directly delete a cookie, but setting its expiry to a past date (e.g. the UNIX epoch) ensures that the cookie will be invalidated (whether and when it will be actually deleted by the browser seems to be browser-dependent).

Helpers and advanced interface (re-exported from cookie)

2 declarations

cookie configuration

datadata SetCookie
#

Data type representing the key-value pair to use for a cookie, as well as configuration options for it.

Creating a SetCookie

SetCookie does not export a constructor; instead, use defaultSetCookie and override values (see http://www.yesodweb.com/book/settings-types for details):

import Web.Cookie
:set -XOverloadedStrings
let cookie = defaultSetCookie { setCookieName = "cookieName", setCookieValue = "cookieValue" }
Cookie Configuration

Cookies have several configuration options; a brief summary of each option is given below. For more information, see RFC 6265 or Wikipedia.

Instances6Eq, Show, NFData, Default, FromHttpApiData, ToHttpApiData
  • Eq SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • Show SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • NFData SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • Default SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • FromHttpApiData SetCookieDefined in http-api-data-0.7 · Web.Internal.HttpApiData

    Note: this instance works correctly for alphanumeric name and value

    Example1 expression
    parseUrlPiece "SESSID=r2t5uvjq435r4q7ib3vtdjq120" :: Either Text SetCookieRight (SetCookie {setCookieName = "SESSID", setCookieValue = "r2t5uvjq435r4q7ib3vtdjq120", setCookiePath = Nothing, setCookieExpires = Nothing, setCookieMaxAge = Nothing, setCookieDomain = Nothing, setCookieHttpOnly = False, setCookieSecure = False, setCookieSameSite = Nothing, setCookiePartitioned = False})
  • ToHttpApiData SetCookieDefined in http-api-data-0.7 · Web.Internal.HttpApiData

    Note: this instance works correctly for alphanumeric name and value

    Example2 expressions
    let Right c = parseUrlPiece "SESSID=r2t5uvjq435r4q7ib3vtdjq120" :: Either Text SetCookietoUrlPiece c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"
    Example1 expression
    toHeader c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"

The URL path for which the cookie should be sent. Default value: Nothing (The browser defaults to the path of the request that sets the cookie).

The time at which to expire the cookie. Default value: Nothing (The browser will default to expiring a cookie when the browser is closed).

The maximum time to keep the cookie, in seconds. Default value: Nothing (The browser defaults to expiring a cookie when the browser is closed).

Directs the browser to not send the cookie for any cross-site request, including e.g. a user clicking a link in their email to open a page on your site.