Skip to content

Instantly share code, notes, and snippets.

@micha
Created December 18, 2018 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micha/d941ff52eaf0f4539074416265c70a47 to your computer and use it in GitHub Desktop.
Save micha/d941ff52eaf0f4539074416265c70a47 to your computer and use it in GitHub Desktop.
(ns fjorm.http
(:require
[cheshire.core :as json]))
(defmacro with-open-> [stream & body]
`(with-open [stream# ~stream]
(-> stream# ~@(map #(if (seq? %) % (list %)) body))))
(defn post-json [url json]
(let [bytes (.getBytes (json/generate-string json))]
(-> (doto (.openConnection (java.net.URL. url))
(.setRequestMethod "POST")
(.setRequestProperty "content-type" "application/json")
(.setDoOutput true)
(.setInstanceFollowRedirects false)
(.setFixedLengthStreamingMode (count bytes))
(.connect)
(-> (.getOutputStream) (with-open-> (.write bytes))))
(.getInputStream)
(java.io.InputStreamReader.)
(with-open-> (json/parse-stream)))))
(comment
(post-json
"http://engine.adzerk.net/api/v2"
{:placements [{:siteId 333999 :adTypes [5]}]})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment