Next: , Previous: , Up: Top   [Contents]

20 Cookies

You can use #:cookies mode to define a URL rule handler.

(get "/certain-rule" #:cookies mode
     (lambda (rc) ...))

mode can be:

Cookie object is used to create/modify the cookie that will return to the client automatically. This is the instance of a cookie which can be controlled in server-side.

If you want to get the cookie from the client, you should use :cookies-copy.

And the APIs:

(:cookies-set! rc cookie-name key val)

(:cookies-ref rc cookie-name key)

(:cookies-setattr! rc cookie-name #:expir #f #:domain #f
                   #:path #f #:secure #f #:http-only #f)

(:cookies-remove! rc key) ; remove cookie from client
(:cookies-copy rc name) ; get cookie from the client

For example:

(get "/cookie" #:cookies '(names cc)
     (lambda (rc)
       (:cookies-set! rc 'cc "sid" "123321")
       "ok"))

(get "/cookie/:expires" #:cookies '(names cc)
     (lambda (rc)
       ;; link the cookie object with actual cookie
       (:cookies-set! rc 'cc "sid" (:cookies-value rc "sid"))
       (:cookies-setattr! rc 'cc #:expir (string->number (params rc "expires")))
       "ok"))

You should link the actual cookie "sid" to the created cookie ’cc, then you can control it. Otherwise it doesn’t work.

You can use these commands in your console to see the results:

curl --head localhost:3000/cookie
# and
curl --head localhost:3000/cookie/120