Last active
May 20, 2017 13:32
-
-
Save mks-m/d7725767dc0425a7bcf9 to your computer and use it in GitHub Desktop.
Convert curl command into http request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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