@@ -4,5 +4,35 @@ by Dan Larkin and Phil Hagelberg
44
55A work in progress.
66
7- Hopefully when this library is ready it can be moved to
8- clojure-contrib; for now it's here.
7+ There are two namespaces, clojure.http.client, which provides a simple
8+ "request" function, and clojure.http.resourcefully, which is targeted
9+ more towards interactions with REST-based APIs.
10+
11+ (ns clojure.http.example
12+ (:use [clojure.http.client]
13+ [clojure.contrib.json.write])
14+ (:require [clojure.http.resourcefully :as resourcefully]))
15+
16+ (let [response (request "http://google.com")]
17+ (:code response) ;; 200
18+ (:msg response) ;; "OK"
19+ (:body-seq response)) ;; ("<html><head><meta[...]" ...
20+
21+ (resourcefully/put "http://localhost:5984/my-db/doc1"
22+ {} (json-str {:hello "world"}))
23+
24+ (resourcefully/with-cookies
25+ (resourcefully/post "http://localhost:3000/login"
26+ {} {"user" user "password" password})
27+ (resourcefully/get "http://localhost:3000/my-secret-page))
28+
29+ The functions in resourcefully are named after the HTTP verbs. Note
30+ that resourcefully must be required : as something since it defines a
31+ "get" function, which would interfere with core if it were fully
32+ referred. Exceptions will be raised for status codes that indicate
33+ problems, so you don't have to check return codes manually. If you use
34+ resourcefully inside a "with-cookies" block, cookies will
35+ automatically be saved in a * cookies* ref and sent out with each
36+ request.
37+
38+ Licensed under the same terms as Clojure.
0 commit comments