4

For example, I need to parse data from my imdb.com account. So, the task is get currently logged page. I taken the "id" cookie from browser. And make GET request, but it doesn't works.

(ns imdb.core
  (:require [clj-http.client :as client])
(def ^:dynamic *base-url* "http://www.imdb.com/")
(def id {"id" {:value "my_value"
               :domain "imdb.com"
               :secure true
               :max-age 3600}})
(defn get-my-page []
  (client/get *base-url* {:cookies id}))

I think I need to set my id cookie to clj-http.cookies/cookie-store. But how?

Tim
  • 96
  • 5

1 Answers1

4

clj-http provides clj-http.cookies namespace which you need to use.

You can setup your cookie store, then call clj-http.cookies/add-cookie with your cookie extracted from browser and finally clj-http.client/get with setting :cookie-store.

On the other hand you might just maintain cookies across multiple HTTP calls as described in the documentation and just call the login page from clj-http directly. It will make your logic repeatable without manual steps like copying cookies from browser.

Piotrek Bzdyl
  • 12,965
  • 1
  • 31
  • 49
  • Thank you, Piotrek! I have no idea why `clj-http.cookies/add-cookie` is unavailable when I use [clj-http "3.1.0"] and "require" for `clj-http.cookies` of course. So I'm pull source code defn to my code, but it also doesn't work: _ClassCastException clojure.lang.PersistentArrayMap cannot be cast to org.apache.http.cookie.Cookie imdb.core/add-cookie (core.clj:35)_ My def for cookie: `(def zz {"zz" {:discard false, :domain "imdb.com", :path "/", :secure false, :value "non"}})` I'm stuck with it ¯\_(ツ)_/¯ – Tim Aug 23 '16 at 18:56
  • 1
    You need to convert your cookie spec into `org.apache.http.cookie ClientCookie` using `clj-http.cookies/to-basic-client-cookie` where your cookie spec is a tuple `[cookie-name cookie-content]`. Did you try to call login endpoint and then GET call with the same cookie store? – Piotrek Bzdyl Aug 23 '16 at 20:13