From eee04eb929baad39b8f46b6158a7d66979e40829 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 19 Sep 2020 15:20:50 +0200 Subject: [PATCH] [#25] Send :form-params application/x-www-form-urlencoded --- src/babashka/curl.clj | 35 +++++++++++++++++++++++++---------- test/babashka/curl_test.clj | 27 +++++++++++++++++++-------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/babashka/curl.clj b/src/babashka/curl.clj index cab4def..49c9dd5 100644 --- a/src/babashka/curl.clj +++ b/src/babashka/curl.clj @@ -77,15 +77,30 @@ (recur (reduce conj! headers* ["-H" (str k ": " v)]) (next kvs))) (persistent! headers*))) accept-header (accept-header opts) - form-params (:form-params opts) - form-params (loop [params* (transient []) - kvs (seq form-params)] - (if kvs - (let [[k v] (first kvs) - v (if (file? v) (str "@" (.getPath ^File v)) v) - param ["--form" (str k "=" v)]] - (recur (reduce conj! params* param) (next kvs))) - (persistent! params*))) + form-params (when-let [form-params (:form-params opts)] + (loop [params* (transient []) + kvs (seq form-params)] + (if kvs + (let [[k v] (first kvs) + v (if (file? v) (str "@" (.getPath ^File v)) v) + param ["--data" (str (url-encode k) "=" (url-encode v))]] + (recur (reduce conj! params* param) (next kvs))) + (persistent! params*)))) + ;; TODO: + ;; multipart-params (when-let [multipart (:multipart opts)] + ;; (loop [params* (transient []) + ;; xs (seq multipart)] + ;; (if xs + ;; (let [x (first xs) + ;; [k v] (if (vector? x) + ;; x + ;; [(or (:part-name x) + ;; (:name x)) + ;; (:content x)]) + ;; v (if (file? v) (str "@" (.getPath ^File v)) v) + ;; param ["--form" (str k "=" v)]] + ;; (recur (reduce conj! params* param) (next xs))) + ;; (persistent! params*)))) query-params (when-let [qp (:query-params opts)] (loop [params* (transient []) kvs (seq qp)] @@ -123,7 +138,7 @@ stream? (identical? :stream (:as opts))] [(conj (reduce into ["curl" "--silent" "--show-error" "--location" "--dump-header" header-file] [method headers accept-header data-raw in-file in-stream basic-auth - form-params + form-params #_multipart-params ;; tested with SSE server, e.g. https://github.com/enkot/SSE-Fake-Server (when stream? ["-N"]) (:raw-args opts)]) diff --git a/test/babashka/curl_test.clj b/test/babashka/curl_test.clj index 6f4b24e..ce477ac 100644 --- a/test/babashka/curl_test.clj +++ b/test/babashka/curl_test.clj @@ -59,18 +59,29 @@ "babashka.curl"))) (testing "form-params" (let [body (:body (curl/post "https://postman-echo.com/post" - {:form-params {"name" "Michiel Borkent"}}))] - (is (str/includes? body "Michiel Borkent")) - (is (str/starts-with? body "{"))) - (testing "form-params from file" + {:form-params {"name" "Michiel Borkent"}})) + body (json/parse-string body true) + headers (:headers body) + content-type (:content-type headers)] + (is (= "application/x-www-form-urlencoded" content-type)))) + ;; TODO: + #_(testing "multipart" + (testing "posting file" (let [tmp-file (java.io.File/createTempFile "foo" "bar") _ (spit tmp-file "Michiel Borkent") _ (.deleteOnExit tmp-file) body (:body (curl/post "https://postman-echo.com/post" - {:form-params {"file" (io/file tmp-file) - "filename" (.getPath tmp-file)}}))] - (is (str/includes? body "foo")) - (is (str/starts-with? body "{")))))) + {:multipart [{:name "file" + :content (io/file tmp-file)} + {:name "filename" :content (.getPath tmp-file)} + ["file2" (io/file tmp-file)]]})) + body (json/parse-string body true) + headers (:headers body) + content-type (:content-type headers)] + (is (str/starts-with? content-type "multipart/form-data")) + (is (:files body)) + (is (str/includes? (-> body :form :filename) "foo")) + (prn body))))) (deftest patch-test (is (str/includes?