A zero dependency HTTP library built on java.net.http.HttpClient.
The java.net.http.HttpClient was added in Java 11. It can be used to request HTTP resources over the network.
It supports both HTTP/1.1 and HTTP/2, synchronous and asynchronous programming models, handles request and response bodies as reactive-streams, and follows the familiar builder pattern.
This library exposes the native java HTTP client via Clojure and supports both sync and async requests.
In order to use this library you will need to be running Java 11+.
Add the library to your project:
[com.owainlewis/java-http-clj "0.3.0"]
You can import the library into your projects as follows:
(ns myns
(:require [java-http-clj.core :as http]))
Requests work exactly as you'd expect.
;; Make a simple request and return the HTTP status
(-> (http/get "http://owainlewis.com") :status)
;; Passing in other request options
(http/get "https://www.google.com"
{:headers {"Accept" "application/json" "Accept-Encoding" ["gzip" "deflate"]}
:timeout 2000})
Alternatively you can construct a raw
request yourself.
Raw requests are created as follows
(http/request {:method :get :url "http://ip.jsontest.com/"})
You can add query parameters to your request using the following form:
(http/get "https://postman-echo.com/get" {:query-parameters {:foo "bar"}})
There are async implementations of all core HTTP methods.
When using the async client, you can provide an optional callback and an optional error handler.
;; With callback
(http/async-get "https://github.com"
(fn [r] (println (:status r))))
;; With callback and error handler
(http/async-get "https://github.com"
(fn [r] (println (:status r)))
(fn [e] (println e)))
Copyright © 2020 Owain Lewis
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php