Book Image

Clojure Web Development Essentials

By : Ryan Baldwin
Book Image

Clojure Web Development Essentials

By: Ryan Baldwin

Overview of this book

Table of Contents (19 chapters)
Clojure Web Development Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Cookies


Show me a website without any cookies, and I'll show you a pamphlet. At some point, you're going to need to write a cookie. Indeed, we've already seen that Ring writes out a cookie for us right out of the box for session tracking.

We can interact with cookies using the noir.cookies namespace. At its most basic, noir.cookies provides two functions: get and put!.

Getting a cookie's value

We can get a cookie's value by calling noir.cookies/get and providing either a string or a keyword for the name of the cookie to retrieve. We can see this in action by creating a trivial route that simply renders out the value of the ring-session cookie:

(require '[compojure.core :refer :all]
         '[noir.cookies :as cookies])
(defroutes some-route
  (GET "/ring-session" [] (cookies/get "ring-session"))   ;#1

The call to cookies/get retrieves the value of the cookie in question – if one exists – otherwise nil. In our case, the preceding code would render something similar to the following:

Setting a cookie...