API for http.agent
- ()
by Stuart Sierra
clojure-contrib is now deprecated
clojure-contrib is no longer being developed or maintained.
Rather than a single, monolithic, contributions library, Clojure now has
a set of separate libraries for each unit of functionality. The libraries
are in the Clojure GitHub organization at
https://github.com/clojure.
API documentation of the libraries can be found at
https://clojure.github.io.
If you're looking for a specific function or namespace from the old
clojure-contrib, see
"Where Did Clojure.Contrib Go".
Full namespace name:
clojure.contrib.http.agent
Overview
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.duck-streams :as d])
(http-agent "http..."
:handler (fn [agnt]
(with-open [w (d/writer "/tmp/out")]
(d/copy (stream agnt) w))))
Public Variables and Functions
buffer-bytes
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (buffer-bytes http-agnt)
The default HTTP agent result handler; it collects the response
body in a java.io.ByteArrayOutputStream, which can later be
retrieved with the 'stream', 'string', and 'bytes' functions.
Source
bytes
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (bytes http-agnt)
Returns a Java byte array of the content returned by the server;
nil if the content is not yet available.
Source
client-error?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (client-error? http-agnt)
Returns true if the HTTP response code was in the 400-499 range.
Source
done?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (done? http-agnt)
Returns true if the HTTP request/response has completed.
Source
error?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (error? http-agnt)
Returns true if the HTTP response code was in the 400-499 range OR
the 500-599 range.
Source
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (headers http-agnt)
Returns a map of HTTP response headers. Header names are converted
to keywords in all lower-case Header values are strings. If a
header appears more than once, only the last value is returned.
Source
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (headers-seq http-agnt)
Returns the HTTP response headers in order as a sequence of
[String,String] pairs. The first 'header' name may be null for the
HTTP status line.
Source
http-agent
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (http-agent uri & options)
Creates (and immediately returns) an Agent representing an HTTP
request running in a new thread.
options are key/value pairs:
:method string
The HTTP method name. Default is "GET".
:headers h
HTTP headers, as a Map or a sequence of pairs like
([key1,value1], [key2,value2]) Default is nil.
:body b
HTTP request entity body, one of nil, String, byte[], InputStream,
Reader, or File. Default is nil.
:connect-timeout int
Timeout value, in milliseconds, when opening a connection to the
URL. Default is zero, meaning no timeout.
:read-timeout int
Timeout value, in milliseconds, when reading data from the
connection. Default is zero, meaning no timeout.
:follow-redirects boolean
If true, HTTP 3xx redirects will be followed automatically. Default
is true.
:handler f
Function to be called when the HTTP response body is ready. If you
do not provide a handler function, the default is to buffer the
entire response body in memory.
The handler function will be called with the HTTP agent as its
argument, and can use the 'stream' function to read the response
body. The return value of this function will be stored in the state
of the agent and can be retrieved with the 'result' function. Any
exceptions thrown by this function will be added to the agent's
error queue (see agent-errors). The default function collects the
response stream in a memory buffer.
Source
message
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (message http-agnt)
Returns the HTTP response message (e.g. 'Not Found'), for this
request, or nil if the response has not yet been received.
Source
method
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (method http-agnt)
Returns the HTTP method name used by this HTTP agent, as a String.
Source
redirect?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (redirect? http-agnt)
Returns true if the HTTP response code was in the 300-399 range.
Note: if the :follow-redirects option was true (the default),
redirects will be followed automatically and a the agent will never
return a 3xx response code.
Source
request-body
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (request-body http-agnt)
Returns the HTTP request body given to this HTTP agent.
Note: if the request body was an InputStream or a Reader, it will no
longer be usable.
Source
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (request-headers http-agnt)
Returns the request headers specified for this HTTP agent.
Source
request-uri
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (request-uri http-agnt)
Returns the URI/URL requested by this HTTP agent, as a String.
Source
result
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (result http-agnt)
Returns the value returned by the :handler function of the HTTP
agent; blocks until the HTTP request is completed. The default
handler function returns a ByteArrayOutputStream.
Source
server-error?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (server-error? http-agnt)
Returns true if the HTTP response code was in the 500-599 range.
Source
status
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (status http-agnt)
Returns the HTTP response status code (e.g. 200, 404) for this
request, as an Integer, or nil if the status has not yet been
received.
Source
stream
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (stream http-agnt)
Returns an InputStream of the HTTP response body. When called by
the handler function passed to http-agent, this is the raw
HttpURLConnection stream.
If the default handler function was used, this function returns a
ByteArrayInputStream on the buffered response body.
Source
string
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (string http-agnt)
(string http-agnt encoding)
Returns the HTTP response body as a string, using the given
encoding.
If no encoding is given, uses the encoding specified in the server
headers, or clojure.contrib.duck-streams/*default-encoding* if it is
not specified.
Source
success?
function
This library, clojure-contrib, is deprecated. See here for more information.
Usage: (success? http-agnt)
Returns true if the HTTP response code was in the 200-299 range.
Source