Using clj-http.cookies to set cookie in request

443 views Asked by At

I am trying to create a client cookie for clj-http so that I can set a cookie for a request. Currently I have the cookie data in the format [cookie-name cookie-val]

I then call to-basic-client-cookie like so:

(clj-http.cookies/to-basic-client-cookie [cookie-name cookie-val])

However, this produces a null pointer exception. Anyone know if I am calling this wrong? Pretty new to clojure, so sorry if this is a dumb question.

1

There are 1 answers

0
jas On

It's looks like the value should be a map, and the map must at least contain the :value key:

user> (cookies/to-basic-client-cookie  ["foo" {:value  "bar"}])
#object[org.apache.http.impl.cookie.BasicClientCookie2 0x1d0338fc " 
[version: 0][name: foo][value: bar][domain: null][path: null][expiry: null]"]

In the code you can see all the other possible cookie content keys:

(defn ^BasicClientCookie2
  to-basic-client-cookie
  "Converts a cookie seq into a BasicClientCookie2."
  [[cookie-name cookie-content]]
  (doto (BasicClientCookie2. (name cookie-name)
                             (name (:value cookie-content)))
    (.setComment (:comment cookie-content))
    (.setCommentURL (:comment-url cookie-content))
    (.setDiscard (:discard cookie-content true))
    (.setDomain (:domain cookie-content))
    (.setExpiryDate (:expires cookie-content))
    (.setPath (:path cookie-content))
    (.setPorts (int-array (:ports cookie-content)))
    (.setSecure (:secure cookie-content false))
    (.setVersion (:version cookie-content 0))))