使用clj-http.cookies在请求中设置cookie

时间:2018-05-22 22:50:50

标签: cookies clojure clj-http

我正在尝试为clj-http创建一个客户端cookie,以便我可以为请求设置cookie。目前我有[cookie-name cookie-val]

格式的cookie数据 然后我按照这样的方式调用-basic-client-cookie:

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

但是,这会产生空指针异常。任何人都知道我是否称错了?对于clojure来说很新,如果这是一个愚蠢的问题,那就很抱歉。

1 个答案:

答案 0 :(得分:2)

看起来该值应该是地图,并且地图必须至少包含:value键:

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]"]

在代码中,您可以看到所有其他可能的Cookie内容键:

(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))))