Liking cljdoc? Tell your friends :D

java-http-clj

A zero dependency HTTP library built on java.net.http.HttpClient.

Getting started

Clojars Project

CircleCI

Intro

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.

Getting started

In order to use this library you will need to be running Java 11+.

You can import the library into your projects as follows:

(ns myns
  (:require [java-http-clj.core :as http]))

Basic Usage

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/"})

Query Parameters

You can add query parameters to your request using the following form:

(http/get "https://postman-echo.com/get" {:query-parameters {:foo "bar"}})

Async Requests

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)))

License

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