2

I've just seen this library https://github.com/technomancy/clojure-http-client with this snippet of code on the README page which is what I'm looking to do

(res/with-cookies {}
  (res/post "http://localhost:3000/login" {} {"user" user "password" password})
  (res/get "http://localhost:3000/my-secret-page))

However it appears that the lib is deprecated and it advises you to use the clj-http library instead. I'm just wondering if anyone knows how to replicate this sort of behaviour using that library?

At the moment I just do

(post "<site i want to login to>" {:form-params {:username "<my username>" :password "<my password>"}})

Which returns a cookie that has a http 302 redirect to the authenticated page, but I have no idea how to make the client follow this redirect using the authenticated cookie

Any help would be appreciated.

2 Answers 2

4

FYI I resolved this,

(defn login [login-url user pass]
  (let [result (client/post "http://my-site.com/login" {:form-params {:username user :password pass}})]
    (when (= (:status result) 302)
      (:cookies result))))

Should login be successful it will return a cookie map, this can then be used in subsequent requests when visiting pages that require you to be logged in, e.g.

(when-let [cookies (login "http://my-site.com" "my-user" "my-pass")]
  (client/get "http://my-site.com/user-page" { :cookies cookies }))
 => <html><head><title>Hello my-user!</titl.......
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you need to explicitly use follow-redirect function from the library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.