Skip to content
Browse files

Enhance processing of sequential query parameters. This could alter t…

…he behavior of client applications in some circumstances.
  • Loading branch information...
1 parent f5f1397 commit e4e929e5d99e61e1839dbc5c4dae0fed98275226 Richard Newman committed
Showing with 29 additions and 3 deletions.
  1. +12 −0 README.markdown
  2. +17 −3 src/com/twinql/clojure/http.clj
View
12 README.markdown
@@ -71,7 +71,19 @@ You can specify the format in which you wish to receive body content and
headers using the `:as` and `:headers-as` keyword arguments. See below for
details.
+# Query parameters *
+Query parameters (as supplied to the `:query` argument) should be associative:
+either a map or a sequence of pairs. Parameters will be processed with
+`as-str`. Non-sequential values will also be processed with `as-str`;
+sequential values (such as vectors) will be turned into multiple query
+parameters, as expected by most HTTP servers. For example:
+
+ (encode-query {:foo "bar" :baz ["noo" 5 true] :top {:x 5 :y 7}})
+ =>
+ "foo=bar&baz=noo&baz=5&baz=true&top=%7B%3Ax+5%2C+%3Ay+7%7D"
+
+
# Examples #
(:content (http/get (java.net.URI. "http://example.com") :as :string))
View
20 src/com/twinql/clojure/http.clj
@@ -33,10 +33,24 @@
(str x)))
(defn- map->name-value-pairs
- "Take an associative structure and return a sequence of BasicNameValuePairs."
+ "Take an associative structure and return a sequence of BasicNameValuePairs.
+ Any associated value that is sequential will appear multiple times in the output, so that
+
+ {:foo [\"bar\" \"baz\"]}
+
+ will produce pairs that encode to
+
+ ?foo=bar&foo=baz"
[q]
- (map (fn [[param value]]
- (new BasicNameValuePair (as-str param) (as-str value)))
+ (mapcat
+ (fn [[param value]]
+ (if (and (sequential? value)
+ (not (empty? value)))
+ (map (partial (fn [#^String p v]
+ (new BasicNameValuePair p (as-str v)))
+ (as-str param))
+ value)
+ [(new BasicNameValuePair (as-str param) (as-str value))]))
q))
(defn encode-query [q]

0 comments on commit e4e929e

Please sign in to comment.
Something went wrong with that request. Please try again.