1.2.0 permalink Arrow_down_16x16

DocString

Agent-based asynchronous HTTP client.

This is a HTTP client library based on Java's HttpURLConnection
class and Clojure's Agent system. It allows you to make multiple
HTTP requests in parallel.

Start an HTTP request with the 'http-agent' function, which
immediately returns a Clojure Agent. You will never deref this
agent; that is handled by the accessor functions. The agent will
execute the HTTP request on a separate thread.

If you pass a :handler function to http-agent, that function will be
called as soon as the HTTP response body is ready. The handler
function is called with one argument, the HTTP agent itself. The
handler can read the response body by calling the 'stream' function
on the agent.

The value returned by the handler function becomes part of the state
of the agent, and you can retrieve it with the 'result' function.
If you call 'result' before the HTTP request has finished, it will
block until the handler function returns.

If you don't provide a handler function, the default handler will
buffer the entire response body in memory, which you can retrieve
with the 'bytes', 'string', or 'stream' functions. Like 'result',
these functions will block until the HTTP request is completed.

If you want to check if an HTTP request is finished without
blocking, use the 'done?' function.

A single GET request could be as simple as:

(string (http-agent "http://www.stuartsierra.com/"))

A simple POST might look like:

(http-agent "http..." :method "POST" :body "foo=1")

And you could write the response directly to a file like this:

(require '[clojure.contrib.io :as d])

(http-agent "http..."
:handler (fn [agnt]
(with-open [w (d/writer "/tmp/out")]
(d/copy (stream agnt) w))))

Vars in clojure.contrib.http.agent