Skip to content

Commit

Permalink
[babashka#25] Send :form-params application/x-www-form-urlencoded
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Sep 19, 2020
1 parent ca177d0 commit eee04eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
35 changes: 25 additions & 10 deletions src/babashka/curl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)])
Expand Down
27 changes: 19 additions & 8 deletions test/babashka/curl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit eee04eb

Please sign in to comment.