HTTP/2 Client


#1

Hi,

I’m interested in putting together a small app which would require an HTTP/2 client. There are a lot of references to HTTP/2 servers around, but http-kit and clj-http both appear to provide HTTP/1.1 clients.

Is anyone aware of an option I’ve missed?


#2

ClojureScript and run Node.js HTTPS server? https://stackoverflow.com/questions/5998694/how-to-create-an-https-server-in-node-js


#3

Unfortunately I’m not seeking a server, just a client.


#4

This? https://github.com/axios/axios it runs in both browsers and Node.js.


#5

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.


#6

Thanks all!

I haven’t spent much time in Java but thheller’s advise looks like the resolution.


#7

And horribly verbose though. Yes, you can abstract over this but then you realized you are spending 70% writing interop abstractions instead of business logic. Of course, this is a case where the libs I need are available in Java but not Clojure.

Writing these for clojure beginners to make them aware that the phrase “You can leverage all the Java ecosystem” is not all roses.


#8

Yes, the API doesn’t look like Clojure. For a Java API it is pretty clean and the point is that you do NOT need to abstract it in any way. You can use it directly. The whole point of Clojure is to “embrace the host” and not reinvent everything just because.

Given how “new” Java11 is and how many people are still on Java8 I’d expect a Clojure wrapper for java.net.http at some point. Clojure devs that don’t know Java may be more comfortable with that and that is fine.

HTTP requests can be an incredibly complex subject if you want to do them at scale (thousands, millions, …). Many client libs choose to hide some of the complexities and simplify or compromise in certain situations and that is fine too. Use the right tool for the job …

Was one of the major selling points of Clojure to me. The majority and Clojure beginners also still come from Java (see Q3 in the recent survey results). YMMV.