Skip to content

Commit

Permalink
Made url-encode polymorhpic, taking string or map as arg.
Browse files Browse the repository at this point in the history
This makes url-encoding public, & useful for encoding request bodies as
well as query strings.
  • Loading branch information
pjt committed May 24, 2009
1 parent f0fca86 commit 9947078
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/clojure/http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

(defn url-encode
"Wrapper around java.net.URLEncoder returning a (UTF-8) URL encoded
representation of text."
[text]
(URLEncoder/encode text "UTF-8"))

(defn- encode-body-map
"Turns a map into a URL-encoded string suitable for a request body."
[body]
(str-join "&" (map #(str-join "=" (map url-encode %)) body)))
representation of argument, either a string or map."
[arg]
(if (map? arg)
(str-join \& (map #(str-join \= (map url-encode %)) arg))
(URLEncoder/encode (as-str arg) "UTF-8")))

(defn- send-body
[body connection headers]
Expand All @@ -36,7 +33,7 @@ representation of text."
(let [out (.getOutputStream connection)]
(cond
(string? body) (spit out body)
(map? body) (spit out (encode-body-map body))
(map? body) (spit out (url-encode body))
(instance? InputStream body) (let [bytes (make-array Byte/TYPE 1000)]
(loop [bytes-read (.read body bytes)]
(when (pos? bytes-read)
Expand Down

0 comments on commit 9947078

Please sign in to comment.