Created
December 18, 2018 15:40
-
-
Save micha/d941ff52eaf0f4539074416265c70a47 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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