diff --git a/README.md b/README.md index f38c159..1194e2c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Clojurl -An example Clojure CLI HTTP/S client using GraalVM native image. +An example HTTP/S client CLI using Clojure and GraalVM native image. Generated with [clj.native-cli template](https://github.com/taylorwood/clj.native-cli). Uses deps.edn and [clj.native-image](https://github.com/taylorwood/clj.native-image). @@ -68,17 +68,19 @@ Compile the program with GraalVM `native-image`: $ clojure -A:native-image ``` -Print CLI options: +CLI options: ``` $ ./clojurl -h -u, --uri URI URI of request -H, --header HEADER Request header(s) -d, --data DATA Request data -m, --method METHOD GET Request method e.g. GET, POST, etc. + -o, --output FORMAT edn Output format e.g. edn, hickory -h, --help ``` +Responses can be printed in EDN or Hickory format. -Make a request and the entire response map will be pretty-printed as EDN to stdout: +Make a request and print response to stdout: ``` $ ./clojurl -u https://postman-echo.com/get {:headers @@ -93,7 +95,11 @@ $ ./clojurl -u https://postman-echo.com/get :status 200, :body "{\"args\":{},\"headers\":{\"host\":\"postman-echo.com\",\"accept\":\"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\",\"accept-encoding\":\"gzip, deflate\",\"user-agent\":\"Java/1.8.0_172\",\"x-forwarded-port\":\"443\",\"x-forwarded-proto\":\"https\"},\"url\":\"https://postman-echo.com/get\"}"} -$ ./clojurl -H Accept=application/json -H X-Session-Id=1234 -u https://postman-echo.com/post -m post -d "{'foo':true}" -H Content-Type=application/json +``` +``` +$ ./clojurl -H Accept=application/json -H X-Session-Id=1234 -H Content-Type=application/json \ + -u https://postman-echo.com/post \ + -m post -d "{'foo':true}" {:headers {"content-encoding" "gzip", "content-type" "application/json; charset=utf-8", diff --git a/deps.edn b/deps.edn index 0237b0f..96865bc 100644 --- a/deps.edn +++ b/deps.edn @@ -1,6 +1,7 @@ {:deps {org.clojure/clojure {:mvn/version "1.9.0"} org.clojure/tools.cli {:mvn/version "0.4.1"} - clj-http-lite {:mvn/version "0.3.0"}} + clj-http-lite {:mvn/version "0.3.0"} + hickory {:mvn/version "0.7.1"}} :aliases {:native-image {:main-opts ["-m clj.native-image clojurl" "--report-unsupported-elements-at-runtime" diff --git a/src/clojurl.clj b/src/clojurl.clj index 23ce1b0..6935cae 100644 --- a/src/clojurl.clj +++ b/src/clojurl.clj @@ -2,7 +2,8 @@ (:require [clj-http.lite.client :as http] [clojure.string :as cs] [clojure.tools.cli :as cli] - [clojure.pprint :refer [pprint]]) + [clojure.pprint :refer [pprint]] + [hickory.core :as hick]) (:gen-class)) (set! *warn-on-reflection* true) @@ -18,6 +19,11 @@ ["-m" "--method METHOD" "Request method e.g. GET, POST, etc." :default :get, :parse-fn (comp keyword cs/lower-case) :default-desc "GET"] + ["-o" "--output FORMAT" "Output format e.g. edn, hickory" + :id :output + :parse-fn {"edn" pprint + "hickory" (comp prn hick/as-hickory hick/parse :body)} + :default pprint :default-desc "edn"] ["-h" "--help" :id :help?]]) (defn -main [& args] @@ -25,10 +31,10 @@ (System/setProperty "java.library.path" (str (System/getenv "GRAALVM_HOME") "/jre/lib")) (let [{:keys [options summary]} (cli/parse-opts args cli-options) - {:keys [help? uri method headers data]} options] + {:keys [help? uri method headers data output]} options] (when help? (println summary)) (when uri - (pprint (http/request {:url uri + (output (http/request {:url uri :method (keyword method) :headers headers :body data})))))