A Clojure HTTP client based on the Java 11 HTTP client. This is a drop in replacement for clojure-http and http-kit.
lein with-profiles dev test
The HTTP Client was added in Java 11. It can be used to request HTTP resources over the network. It supports HTTP/1.1 and HTTP/2, both synchronous and asynchronous programming models, handles request and response bodies as reactive-streams, and follows the familiar builder pattern.
This library exposes this Java client via Clojure and supports both sync and async requests. It can be used as a light weight, drop-in replacement for http-kit and clojure-http.
This library requires Java 11+
You will need to import this library first
(ns myns
(:require [java-http-clj.core :as http]))
Raw requests are created as follows
(http/request
{:method :get
:url "http://ip.jsontest.com/"}
{:as :json})
;; If you don't specify any options defaults are used
(http/get "http://www.google.com")
;; With request options
(http/get "https://www.google.com"
{:headers {"Accept" "application/json" "Accept-Encoding" ["gzip" "deflate"]}
:timeout 2000})
(http/post "https://postman-echo.com/post"
{:headers { "Content-Type" "application/json"}
:body "Hello, World!"})
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
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close