Created
January 8, 2022 21:50
-
-
Save mikeananev/0cbe7e7eef3210a9cab7b41fb1fddc67 to your computer and use it in GitHub Desktop.
Example of Java HTTP Client using Clojure
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 org.rssys.lib01 | |
(:import (java.net.http HttpClient HttpClient$Version HttpClient$Redirect | |
HttpResponse$BodyHandlers HttpRequest) | |
(java.time Duration) | |
(java.net URI))) | |
(defn http-get [^String url] | |
(let [client (-> (HttpClient/newBuilder) | |
(.version HttpClient$Version/HTTP_1_1) | |
(.followRedirects HttpClient$Redirect/NORMAL) | |
(.connectTimeout (Duration/ofSeconds 20)) | |
.build) | |
request (-> (HttpRequest/newBuilder (URI. url)) | |
.build) | |
response (.send client request (HttpResponse$BodyHandlers/ofString))] | |
{:status (.statusCode response) | |
:body (.body response)})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment