Skip to content
weavejester edited this page Oct 21, 2011 · 22 revisions

Sessions in Ring work a little differently than you might expect, because Ring attempts to be functional when possible.

Session data is passed via the request map on the :session key.

(use 'ring.middleware.session
     'ring.util.response)

(defn handler [{session :session}]
  (response (str "Hello " (:username session)))

(def app
  (wrap-session handler))

To change the session data, you can add a :session key to the response that contains the updated session data.

(use 'ring.middleware.session
     'ring.util.response)

(defn handler [{session :session}]
  (let [count (:count session 0)]
    (-> (response (str "You accessed this page " count " times."))
        (assoc-in [:session :count] (inc count)))))

(def app
  (wrap-session handler))
Clone this wiki locally