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:

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-update! rc) ; cookie operations won't work unless you update it

NOTE: You don’t have to call :cookies-update! yourself, since it will be called automatically by the hook before the response.

For example:

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

(get "/cookie/:expires" #:cookies '(names cc)
     (lambda (rc)
       (:cookies-set! rc 'cc "sid" "123321")
       (:cookies-setattr! rc 'cc #:expir (string->number (params rc "expires")))
       "ok"))

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

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