Skip to content

Instantly share code, notes, and snippets.

@dainiusjocas
Last active March 22, 2020 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainiusjocas/d2a5ca60ae25cd260f37b7c125a0b2e6 to your computer and use it in GitHub Desktop.
Save dainiusjocas/d2a5ca60ae25cd260f37b7c125a0b2e6 to your computer and use it in GitHub Desktop.
Function to parse curl response with headers to a map
(defn resp->map [resp]
(let [lines (str/split resp #"\r\n")
status (Integer/parseInt (second (str/split (first lines) #" ")))
headers (reduce (fn [acc header-line]
(let [[k v] (str/split header-line #":" 2)]
(assoc acc (str/lower-case k) (str/trim v))))
{}
(remove str/blank? (drop-last (rest lines))))]
{:body (last lines)
:status status
:headers headers}))
;; example usage
(resp->map
(curl/request {:method :get
:url (str "https://httpstat.us/200")
:headers {"Accept" "application/json"}
:timeout 900000
:raw-args ["-i"]})))
=>
{:body "{\"code\": 200, \"description\": \"OK\"}",
:status 200,
:headers {"access-control-expose-headers" "Request-Context",
"request-context" "appId=cid-v1:7585021b-2db7-4da6-abff-2cf23005f0a9",
"server" "Microsoft-IIS/10.0",
"content-type" "application/json; charset=utf-8",
"access-control-allow-origin" "*",
"content-length" "34",
"x-aspnetmvc-version" "5.1",
"set-cookie" "ARRAffinity=a9a558d85db7c9042f17b30215006a00b4f264f8b043f8bd02b56570806b02c6;Path=/;HttpOnly;Domain=httpstat.us",
"x-aspnet-version" "4.0.30319",
"date" "Sun, 22 Mar 2020 21:29:44 GMT",
"x-powered-by" "ASP.NET",
"cache-control" "private"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment