Compojure/Ring: can't get session working
Question
I'm clearly missing something obvious, but:
(ns session-demo.core
(:gen-class)
(:use compojure.core
ring.middleware.session
[hiccup core page form])
(:require [compojure.route :as route]
[compojure.handler :as handler]))
(defn index-page []
(html5
[:body
(form-to [:get "/status"]
(submit-button "Proceed"))]))
(defn status-page [params]
(html5
[:body
[:p "Session: " (:session params)]
[:p "Params: " params]]))
(defroutes the-routes
(GET "/" [] (index-page))
(GET "/status" {session :session :as params} (status-page params)))
(def app
(-> (handler/site the-routes)
wrap-session))
After clicking "Proceed" button shows:
Session: {}
Params: {:ssl-client-cert nil, :remote-addr "127.0.0.1", :scheme :http, :query-params {}, :session {}, :form-params {}, :multipart-params {}, :request-method :get, :query-string nil, :route-params {}, :content-type nil, :cookies {"org.cups.sid" {:value "72d6fc6a299669e6332da6eb72964f97"}}, :uri "/status", :server-name "localhost", :params {}, :headers {"accept-encoding" "gzip, deflate", "user-agent" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0", "referer" "http://localhost:3000/", "connection" "keep-alive", "accept-language" "en-US,en;q=0.5", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "host" "localhost:3000", "cookie" "org.cups.sid=72d6fc6a299669e6332da6eb72964f97"}, :content-length nil, :server-port 3000, :character-encoding nil, :body #, :flash nil}
I'd expect something meaningful to be set in (:session params)...
Solution
The session starts empty. To put something in the session you need to return a map from your handler, and under the key :session
you should provide the updated session data.
Something like this maybe:
(defn index-page []
{:body (html5
[:body
(form-to [:get "/status"]
(submit-button "Proceed"))])
:session {:test [1 2 3]}})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow