Skip to content

Instantly share code, notes, and snippets.

@keymone keymone/curlj.clj
Last active May 20, 2017

Embed
What would you like to do?
Convert curl command into http request
(def curl-opts
[["-A" "--user-agent AGENT" "User-Agent string"
:assoc-fn (fn [m _ v] (assoc-in m [:headers "User-Agent"] v))]
["-b" "--cookie DATA" "Cookie name=value"
:id :cookies :default {}
:assoc-fn (fn [m k v] (let [[kk & vv] (split v #"=")]
(assoc-in m [k kk] {:discard true
:path "/"
:value (join "=" vv)})))]
["-H" "--header DATA" "Header \"header: value\""
:id :headers
:default {}
:assoc-fn (fn [m k v] (let [[kk & vv] (split v #": ")]
(assoc-in m [k kk] (join ": " vv))))]])
(defmacro curl [& opts]
(let [{opts# :options
[_ req#] :arguments}
(parse-opts (cons "curl" (map str opts))
curl-opts)]
`(clj-http.client/get ~req# ~opts#)))
(curl -A "hello" -H "Host: google.de" -b "Expire-at=today" http://google.com?q=a)
; compiles into
(clj-http.client/get
"http://google.com?q=a"
{:cookies {"Expire-at" {:discard true, :path "/", :value "today"}},
:headers {"User-Agent" "hello", "Host" "google.de"}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.