Skip to content

Instantly share code, notes, and snippets.

@mks-m
Last active May 20, 2017 13:32
Show Gist options
  • Save mks-m/d7725767dc0425a7bcf9 to your computer and use it in GitHub Desktop.
Save mks-m/d7725767dc0425a7bcf9 to your computer and use it in GitHub Desktop.
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