diff --git a/.gitignore b/.gitignore index 23f7de1..7460f86 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ flycheck_*.el **/.clj-kondo/.cache +.idea/ diff --git a/README.md b/README.md index f8339c2..a1dffa0 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,15 @@ Using the low-level API for fine grained(and safer) URL construction: :url "https://httpbin.org/get?q=test"} ``` +### Error output + +Error output can be found under the `:err` key: + +``` clojure +(:err (curl/get "httpx://postman-echo.com/get")) +;;=> "curl: (1) Protocol \"httpx\" not supported or disabled in libcurl\n" +``` + ### Debugging requests Set `:debug` to `true` to get debugging information along with the response. The diff --git a/src/babashka/curl.clj b/src/babashka/curl.clj index e63d5fa..0f69856 100644 --- a/src/babashka/curl.clj +++ b/src/babashka/curl.clj @@ -22,18 +22,20 @@ [args] (let [pb (let [pb (ProcessBuilder. ^java.util.List args)] (doto pb - (.redirectInput ProcessBuilder$Redirect/INHERIT) - (.redirectError ProcessBuilder$Redirect/INHERIT))) + (.redirectInput ProcessBuilder$Redirect/INHERIT))) proc (.start pb) - out (.getInputStream proc)] + out (.getInputStream proc) + err (.getErrorStream proc)] {:out out + :err err :proc proc})) (defn- exec-curl [args opts] (let [res (shell-command args) out (:out res) + err (:err res) proc (:proc res)] - (assoc opts :out out :proc proc))) + (assoc opts :out out :err err :proc proc))) (defn- file? [f] (when (instance? File f) @@ -152,9 +154,11 @@ ;; curl does not write to :header-file until stdout is read from once. ;; This ensures :status and :headers are parsed when option `:as :stream` is set. is (read-then-unread is) - body (if (identical? :stream (:as opts)) - is - (slurp is)) + err (:err opts) + stream? (identical? :stream (:as opts)) + [body err] (if stream? + [is err] + [(slurp is) (slurp err)]) headers (read-headers (:header-file opts)) [status headers] (reduce (fn [[status parsed-headers :as acc] header-line] @@ -169,6 +173,7 @@ response {:status status :headers headers :body body + :err err :process (:proc opts)}] response)) diff --git a/test/babashka/curl_test.clj b/test/babashka/curl_test.clj index cef78e0..abc5575 100644 --- a/test/babashka/curl_test.clj +++ b/test/babashka/curl_test.clj @@ -5,6 +5,10 @@ [clojure.string :as str] [clojure.test :refer [deftest is testing]])) +(defmethod clojure.test/report :begin-test-var [m] + (println "===" (-> m :var meta :name)) + (println)) + (deftest get-test (is (str/includes? (:body (curl/get "https://httpstat.us/200")) "200")) @@ -176,3 +180,8 @@ opts (:options resp)] (is (pos? (.indexOf command "--head"))) (is (identical? :head (:method opts))))) + +(deftest stderr-test + (let [resp (curl/get "blah://postman-echo.com/get")] + (is (contains? resp :err)) + (is (str/starts-with? (:err resp) "curl: (1)"))))