oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
526 stars 45 forks source link

how to add custom cookies using martian and clj-http #84

Closed bhougland18 closed 4 years ago

bhougland18 commented 4 years ago

I apologize for submitting an issue for a question, but how would you add a clj-http cookiestore as an interceptor? I am assuming that is the correct way to accomplish this objective.

To do this using native clj-http one could do:

(def cs (clj-http.cookies/cookie-store))

(def http-client-response (clj-http.client/get "https://httpbin.org/cookies" {:cookie-store cs})))

oliyh commented 4 years ago

Hi @bhougland18

I would approach it like this:

(defn with-cookie-store [cookie-store]
  {:name ::with-cookie-store
   :enter (fn [ctx]
            (assoc-in ctx [:request :cookie-store] cookie-store))})

(defn build-martian []
  (let [cookie-store (clj-http.cookies/cookie-store)]
    (martian.clj-http/bootstrap-swagger "http://swagger.com/swagger.json"
                                        {:interceptors (cons (with-cookie-store cookie-store)
                                                             martian.clj-http/default-interceptors))))

The entire map built up under the :request key is given to clj-http.client/request so by putting your cookie store in there it should work as required.

Hope this helps

oliyh commented 4 years ago

Hi @bhougland18 shall I close the issue?

bhougland18 commented 4 years ago

Oliver,

Yes. Sorry, I have been very busy. I am really grateful for your help.

On Mon, May 11, 2020 at 11:11 AM Oliver Hine notifications@github.com wrote:

Hi @bhougland18 https://github.com/bhougland18 shall I close the issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/oliyh/martian/issues/84#issuecomment-626765056, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGZANCHO5F7LAANNHIU6RELRRAIQTANCNFSM4MT4Q6KQ .

oliyh commented 4 years ago

Great, thanks for letting me know.

tommy-mor commented 2 years ago

This helped me, thank you for the snippet