Last active
August 18, 2021 14:27
-
-
Save sgrove/8afa25ff777b6622a0a6022394688a44 to your computer and use it in GitHub Desktop.
Convert Chrome's `copy-as-curl` to clj-http calls
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
(defn curl->clj* | |
"Given a (single) curl string from Chrome's copy-as-curl, convert it either | |
into a clj-http call, a task-based http entry, or a shortened | |
task-based entry" | |
([curl-str] | |
(curl->clj* curl-str {})) | |
([curl-str conv-opts] | |
(let [clean-opts (:clean conv-opts) | |
merge-opts (:merge conv-opts) | |
headers (-> | |
(->> (re-seq #"-H '([a-zA-Z0-9 \-]+)\:([a-zA-Z0-9 \-\/\*\+\"\(\)\.\;_,=:]+)" curl-str) | |
(mapv (comp vec (partial map string/trim) (partial drop 1))) | |
(into {})) | |
((partial apply dissoc) (:headers clean-opts)) | |
(merge (:headers merge-opts))) | |
url (second (string/split curl-str #"'")) | |
data* (some-> (re-find #"--data '([^']+)'" curl-str) | |
(second)) | |
data (or (some-> data* | |
(string/split #"&") | |
(some->> | |
(mapv #(string/split % #"=")) | |
(into {})) | |
((partial apply dissoc) (:form-params clean-opts)) | |
(merge (:form-params merge-opts))) | |
data*) | |
x-flag (second (re-find #"-X (PUT|POST|GET|PATCH|DELETE|OPTIONS)" curl-str)) | |
method* (cond | |
x-flag x-flag | |
(boolean data) "POST" | |
:else "GET") | |
method (-> method* | |
(string/lower-case) | |
(symbol)) | |
_ (def -bh headers) | |
params-or-body (when data | |
(if (some->> | |
(get headers "Content-Type") | |
(re-find #"application/x-www-form-urlencoded") | |
(boolean)) | |
{:form-params data} | |
{:body data*})) | |
opts (merge {:headers headers} | |
params-or-body)] | |
(condp = (:format conv-opts) | |
`(~(symbol (str "clj-http.client/" method)) ~url | |
~opts))))) | |
(defn curl->clj | |
"Given a curl string from Chrome's copy-as-curl (or | |
copy-all-as-curl), convert it either into clj-http calls, task-based | |
http entries, or shortened task-based entries" | |
[curl-str opts] | |
(->> (string/split curl-str #"\ncurl ") | |
(mapv #(curl->clj* % opts)) )) |
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
(curl->clj "curl 'https://httpbin.org/put' -X PUT -H 'Pragma: no-cache' -H 'Origin: https://resttesttest.com' -H 'Accept-Encoding: gzip, deflate, sdch, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Referer: https://resttesttest.com/' -H 'Connection: keep-alive' --data 'this=that' --compressed" | |
{:clean {:headers ["Accept-Encoding" "Cache-Control" "Cookie" "Pragma"]} | |
:merge {:headers {"User-Agent" `(:user-agent ~'ctx)}}}) | |
(clj-http.client/put | |
"https://httpbin.org/put" | |
{:headers | |
{"Origin" "https://resttesttest.com", | |
"User-Agent" (:user-agent ctx), | |
"Accept-Language" "en-US,en;q=0.8", | |
"Content-Type" "application/x-www-form-urlencoded; charset=UTF-8", | |
"Connection" "keep-alive", | |
"Accept" "*/*", | |
"Referer" "https://resttesttest.com/"}, | |
:body "this=that"}) | |
(curl->clj "curl 'https://httpbin.org/put' -X OPTIONS -H 'Pragma: no-cache' -H 'Access-Control-Request-Method: PUT' -H 'Origin: https://resttesttest.com' -H 'Accept-Encoding: gzip, deflate, sdch, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Referer: https://resttesttest.com/' -H 'Connection: keep-alive' --compressed" | |
{:clean {:headers ["Accept-Encoding" "Cache-Control" "Cookie" "Pragma"]} | |
:merge {:headers {"User-Agent" `(:user-agent ~'ctx)}}}) | |
(clj-http.client/options | |
"https://httpbin.org/put" | |
{:headers | |
{"Origin" "https://resttesttest.com", | |
"User-Agent" (:user-agent ctx), | |
"Accept-Language" "en-US,en;q=0.8", | |
"Access-Control-Request-Method" "PUT", | |
"Connection" "keep-alive", | |
"Accept" "*/*", | |
"Referer" "https://resttesttest.com/"}}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about
clj->curl
? :)