Unable to set cookie in clojure ring

654 Views Asked by At

I'm sending the following response in clojure ring:

(res/set-cookie
         (res/redirect (env :some-url))
         "some-id"
         (->
          req
          foo-ns/bar
          :id
          )
         {:max-age (* 30 24 60 60 1000) :path "/"})

And on printing this response I get:

{:status 302, :headers {"Location" "http://localhost:5000"}, :body "", :cookies {"some-id" {:value "1341313515135490454", :max-age 2592000000, :path "/"}}}

But on the client side, the cookie isn't set, which I can see in the console. What am I doing wrong?

1

There are 1 best solutions below

0
On

It looks like you're using ring.response/set-cookie to set the cookie. That will set the cookie attributes under :cookies in your response map. Before returning the response to the browser, you need to encode those cookies into a Set-Cookie header that the browser can understand. To do this, add the ring.middleware.cookies/wrap-cookies middleware to your middleware stack.

You should expect your response to look something like:

{:status 302
 :body ""
 :headers {"Location" "http://localhost:5000"
           "Set-Cookie" "some-id=1341313515135490454; max-age=2592000000; path=/"}}