Skip to content

Commit 9947078

Browse files
committed
Made url-encode polymorhpic, taking string or map as arg.
This makes url-encoding public, & useful for encoding request bodies as well as query strings.
1 parent f0fca86 commit 9947078

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/clojure/http/client.clj

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111

1212
(defn url-encode
1313
"Wrapper around java.net.URLEncoder returning a (UTF-8) URL encoded
14-
representation of text."
15-
[text]
16-
(URLEncoder/encode text "UTF-8"))
17-
18-
(defn- encode-body-map
19-
"Turns a map into a URL-encoded string suitable for a request body."
20-
[body]
21-
(str-join "&" (map #(str-join "=" (map url-encode %)) body)))
14+
representation of argument, either a string or map."
15+
[arg]
16+
(if (map? arg)
17+
(str-join \& (map #(str-join \= (map url-encode %)) arg))
18+
(URLEncoder/encode (as-str arg) "UTF-8")))
2219

2320
(defn- send-body
2421
[body connection headers]
@@ -36,7 +33,7 @@ representation of text."
3633
(let [out (.getOutputStream connection)]
3734
(cond
3835
(string? body) (spit out body)
39-
(map? body) (spit out (encode-body-map body))
36+
(map? body) (spit out (url-encode body))
4037
(instance? InputStream body) (let [bytes (make-array Byte/TYPE 1000)]
4138
(loop [bytes-read (.read body bytes)]
4239
(when (pos? bytes-read)

0 commit comments

Comments
 (0)