HTTP/2 Client

If you are running Clojure on Java11 you can use the newly added HttpClient which has full support for HTTP/2. The API is a bit java-ish but works well enough from Clojure.

(ns foo.test
  (:import [java.net.http HttpClient HttpRequest HttpClient$Version HttpResponse$BodyHandlers]
           [java.net URI]))

(let [req
      (-> (HttpRequest/newBuilder)
          (.uri (URI/create "https://www.google.com"))
          (.GET)
          (.build))

      client
      (-> (HttpClient/newBuilder)
          (.version HttpClient$Version/HTTP_2)
          (.build))

      res
      (.send client req (HttpResponse$BodyHandlers/ofString))]
  (println (.body res)))

The entire java.net.http package is pretty powerful and supports everything you’ll ever need to do HTTP requests. Assuming of course you are running Java11 and Clojure.

10 Likes