Skip to content

Commit 39dda13

Browse files
committed
Make the caller add query params to the URL instead of doing it in request.
1 parent 677a48d commit 39dda13

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

README.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ more towards interactions with REST-based APIs.
2222
(:body-seq response)) ;; ("<html><head><meta[...]" ...
2323

2424
(resourcefully/put "http://localhost:5984/my-db/doc1"
25-
{} {} (json-str {:hello "world"}))
25+
{} (json-str {:hello "world"}))
2626

2727
(res/with-cookies {}
2828
(res/post "http://localhost:3000/login"
29-
{} {} {"user" user "password" password})
29+
{} {"user" user "password" password})
3030
(res/get "http://localhost:3000/my-secret-page))
3131

3232
The request function requires a URL and optionally accepts a method
33-
(GET by default), a map of headers, a map of cookies, a map of query
34-
parameters, and a request body. The resourcefully functions take a URL,
35-
an optional headers map, and an optional body.
33+
(GET by default), a map of headers, a map of cookies, and a request
34+
body. The resourcefully functions take a URL, an optional headers map,
35+
and an optional body.
3636

3737
Request bodies may be strings, maps, or InputStreams. Strings get sent
3838
verbatim. Maps get sent as application/x-www-form-urlencoded, and

src/clojure_http/client.clj

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,21 @@ by a server."
105105
#^String (as-str (val cookie))))
106106
cookie-map)))
107107

108-
(defn- queryify
109-
"Takes a map of query parameters and turns them into a query string."
110-
[url query-map]
111-
(if (seq query-map)
112-
(apply str url "?" (interpose "&" (for [[k v] query-map] (str (url-encode k) "=" (url-encode v)))))
108+
(defn add-query-params
109+
"Takes a URL and query params and returns a URL with query params attached."
110+
[url & query-params]
111+
(if (seq query-params)
112+
(apply str url "?"
113+
(interpose "&" (for [[k v] query-params]
114+
(str (url-encode k) "=" (url-encode v)))))
113115
url))
114116

115117
(defn request
116118
"Perform an HTTP request on URL u."
117-
[u & [method headers cookies querys body]]
119+
[u & [method headers cookies body]]
118120
;; This function *should* throw an exception on non-HTTP URLs.
119121
;; This will happen if the cast fails.
120-
(let [u (url (queryify u querys))
122+
(let [u (url u)
121123
#^HttpURLConnection connection
122124
(cast HttpURLConnection (.openConnection u))
123125
method (.toUpperCase #^String (as-str (or method

src/clojure_http/resourcefully.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
(defmacro define-method
3737
[method]
3838
`(defn ~method
39-
~(str "Perform HTTP " method " request to url u with specified headers
40-
and query map. Cookies will be saved if inside with-cookies block.")
41-
[u# & [headers# querys# body#]]
39+
~(str "Perform HTTP " method " request to url u with specified headers.
40+
Cookies will be saved if inside with-cookies block.")
41+
[u# & [headers# body#]]
4242
(let [response# (save-cookies (client/request u# ~(str method)
4343
headers# (if *cookies*
4444
@*cookies*)
45-
querys#
4645
body#))]
4746
(if (error? response#)
4847
(throw (java.io.IOException. (error-message response#)))

0 commit comments

Comments
 (0)