Skip to content
weavejester edited this page Oct 16, 2011 · 8 revisions

To add cookie support to your Ring handler, you'll need to wrap it in the wrap-cookies middleware:

(use 'ring.middleware.cookies)
(def app
  (wrap-cookies your-handler))

This adds the :cookies key to the request map, which will contain a map of cookies looking something like this:

{"username" {:value "alice"}}

To set the a cookie, you add a :cookies key to the response map:

{:status 200
 :headers {}
 :cookies {"username" {:value "alice"}}
 :body "Setting a cookie."}

As well as setting the value of the cookie, you can also set additional attributes:

  • :domain - restrict the cookie to a specific domain
  • :path - restrict the cookie to a specific path
  • :port - restrict the cookie to a specific port
  • :secure - restrict the cookie to HTTPS URLs if true
  • :max-age - the number of seconds until the cookie expires
  • :expires - a specific date and time the cookie expires

So if you wanted to have a secure cookie that expires in one hour, you'd use:

{"secret" {:value "foobar", :secure true, :max-age 3600}}