ring-clojure / ring

Clojure HTTP server abstraction
MIT License
3.73k stars 518 forks source link

Documentation for cookies #488

Closed Sleepful closed 4 months ago

Sleepful commented 7 months ago

https://ring-clojure.github.io/ring/ring.middleware.cookies.html#var-wrap-cookies

it says:

To set cookies, add a map to the :cookies key on the response. The values of the cookie map can either be strings, or maps containing the following keys:

however it was not clear, and I tried setting a string for a bit to no avail:

;; errors
((wrap-cookies (constantly {:cookies "foo=bar"})) {})

;; works fine
((wrap-cookies (constantly {:cookies {"foo" "bar"}})) {})

Perhaps just adding an example to the docs+wiki would be nice. I can PR it later.

Sleepful commented 7 months ago

This example would have made it clear:

;; middleware example with ring-anti-forgery
(defn attach-anti-forgery [handler]
  (fn [request]
    (let [response (handler request)
          response (assoc response :cookies {"acsrf" {:value  *anti-forgery-token* :path "/"}})]
      response)))
weavejester commented 7 months ago

Adding extra examples to the Wiki sounds like a good idea, but that doesn't require a PR - you can just edit the Wiki.

I don't see anything wrong with the docstring, though. It says to add a map to the :cookies key, but you added a string. Maybe it would be clearer with an example, but a docstring isn't the place for comprehensive examples; it needs to be relatively concise.