Code Shop

Code Shop

Abhi Yerra  //  An entity composed of reality and pseudo-reality who enjoys coding, news, photography and wasting time.

Oct 1 / 3:24am

Simple Clojure Http Client

I'm learning Clojure and needed a simple http client. Here is the one
I have written using the java libraries.

 
(import '(java.net URL HttpURLConnection) 
 '(java.io InputStreamReader BufferedReader)) 
 
(defn http-client-get-content [reader content] 
 (let [line (.readLine reader)] 
 (if (nil? line) 
 content 
 (recur reader (str content "\n" line))))) 
 
(defn http-client-get [url] 
 (let [connection (.openConnection (URL. url))] 
 (doto connection 
 (.setRequestMethod "GET") 
 (.setDoOutput true) 
 (.setReadTimeout 10000) 
 (.connect)) 
 (http-client-get-content (BufferedReader. (InputStreamReader. 
(.getInputStream connection))) ""))) 

0 comments

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter