Permalink
Browse files

Make the caller add query params to the URL instead of doing it in re…

…quest.
  • Loading branch information...
1 parent 677a48d commit 39dda131f998bbda7d081574ff09c478308b9cc8 @technomancy technomancy committed May 31, 2010
Showing with 17 additions and 16 deletions.
  1. +5 −5 README.markdown
  2. +9 −7 src/clojure_http/client.clj
  3. +3 −4 src/clojure_http/resourcefully.clj
View
@@ -22,17 +22,17 @@ more towards interactions with REST-based APIs.
(:body-seq response)) ;; ("<html><head><meta[...]" ...
(resourcefully/put "http://localhost:5984/my-db/doc1"
- {} {} (json-str {:hello "world"}))
+ {} (json-str {:hello "world"}))
(res/with-cookies {}
(res/post "http://localhost:3000/login"
- {} {} {"user" user "password" password})
+ {} {"user" user "password" password})
(res/get "http://localhost:3000/my-secret-page))
The request function requires a URL and optionally accepts a method
-(GET by default), a map of headers, a map of cookies, a map of query
-parameters, and a request body. The resourcefully functions take a URL,
-an optional headers map, and an optional body.
+(GET by default), a map of headers, a map of cookies, and a request
+body. The resourcefully functions take a URL, an optional headers map,
+and an optional body.
Request bodies may be strings, maps, or InputStreams. Strings get sent
verbatim. Maps get sent as application/x-www-form-urlencoded, and
@@ -105,19 +105,21 @@ by a server."
#^String (as-str (val cookie))))
cookie-map)))
-(defn- queryify
- "Takes a map of query parameters and turns them into a query string."
- [url query-map]
- (if (seq query-map)
- (apply str url "?" (interpose "&" (for [[k v] query-map] (str (url-encode k) "=" (url-encode v)))))
+(defn add-query-params
+ "Takes a URL and query params and returns a URL with query params attached."
+ [url & query-params]
+ (if (seq query-params)
+ (apply str url "?"
+ (interpose "&" (for [[k v] query-params]
+ (str (url-encode k) "=" (url-encode v)))))
url))
(defn request
"Perform an HTTP request on URL u."
- [u & [method headers cookies querys body]]
+ [u & [method headers cookies body]]
;; This function *should* throw an exception on non-HTTP URLs.
;; This will happen if the cast fails.
- (let [u (url (queryify u querys))
+ (let [u (url u)
#^HttpURLConnection connection
(cast HttpURLConnection (.openConnection u))
method (.toUpperCase #^String (as-str (or method
@@ -36,13 +36,12 @@
(defmacro define-method
[method]
`(defn ~method
- ~(str "Perform HTTP " method " request to url u with specified headers
-and query map. Cookies will be saved if inside with-cookies block.")
- [u# & [headers# querys# body#]]
+ ~(str "Perform HTTP " method " request to url u with specified headers.
+Cookies will be saved if inside with-cookies block.")
+ [u# & [headers# body#]]
(let [response# (save-cookies (client/request u# ~(str method)
headers# (if *cookies*
@*cookies*)
- querys#
body#))]
(if (error? response#)
(throw (java.io.IOException. (error-message response#)))

0 comments on commit 39dda13

Please sign in to comment.