Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add cookies to the cookie store #600

Open
bryanhonof opened this issue Jul 26, 2021 · 1 comment
Open

Unable to add cookies to the cookie store #600

bryanhonof opened this issue Jul 26, 2021 · 1 comment

Comments

@bryanhonof
Copy link

I'm trying to manually add a cookie to a cookie store, but I'm running into some problems.

Running the following code results in an empty set

(def my-cookie-store (clj-http.cookies/cookie-store))
(clj-http.cookies/add-cookie my-cookie-store (clj-http.cookies/to-basic-client-cookie ["foo" {:value "bar"}]))
(clojure.pprint/pprint (clj-http.cookies/get-cookies my-cookie-store))

=> {}

Whilst running the following results in the expected set of 1 cookie.

(def my-cookie-store (clj-http.cookies/cookie-store))
(clj-http.cookies/add-cookie my-cookie-store (org.apache.http.impl.cookie.BasicClientCookie2. "foo" "bar"))
(clojure.pprint/pprint (clj-http.cookies/get-cookies my-cookie-store))

=> {"foo" {:discard true, :secure false, :value "bar", :version 0}}

Am I using the add-cookie, or to-basic-client-cookie, wrong? Because as far as I understand the source code, both versions of my code should achieve the same results.

@rdivyanshu
Copy link

rdivyanshu commented Jul 27, 2021

@bryanhonof Reason is to-basic-client-cookie set discard to true https://github.com/dakrone/clj-http-async/blob/877ee3b0dd3d586281e3528a5386ad4bd1f68a7d/src/clj_http/cookies.clj#L50

user=> (def c (clj-http.cookies/to-basic-client-cookie ["foo" {:value "bar"}]))
#'user/c
user=> (.isExpired c (new java.util.Date))
true
user=> (def d (org.apache.http.impl.cookie.BasicClientCookie2. "foo" "bar"))
#'user/d
user=> (.isExpired d (new java.util.Date))
false
user=> (.getExpiryDate c)
nil
user=> (.getExpiryDate d)
nil
user=>

https://github.com/apache/httpcomponents-client/blob/dd76cf91c8f9125c7e0e29770622271f9417b46d/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java#L90

https://github.com/apache/httpcomponents-client/blob/dd76cf91c8f9125c7e0e29770622271f9417b46d/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java#L88

It is showing discard true because it is calling (.isPersistent cookie) which is false in both cases, discard is private variable

:discard (not (.isPersistent cookie))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants