/hop

Minimal wrapper around the JDK HttpClient

Primary LanguageClojureMIT LicenseMIT

Hop

Minimal wrapper for Java's HttpClient.

Status

Run tests

Currently for my personal use. Future breaking changes possible.

Deps

Add to deps.edn

{:deps
  {dev.onionpancakes/hop
    {:git/url   "https://github.com/onionpancakes/hop"
     :git/sha   "<GIT SHA>"}}}

Examples

GET

(require '[dev.onionpancakes.hop.client :as hop])

(hop/send "http://www.example.com")

(hop/send {:uri "http://www.example.com"} :input-stream)

POST

(hop/send {:method :POST
           :uri    "http://www.example.com"
           :body   "my post data"})

Usage

Require the namespace.

(require '[dev.onionpancakes.hop.client :as hop])

Send Requests

Send some request.

  • First arg is a request map. Strings are accepted as shorthand as {:uri <str value>}.
  • Second arg is a BodyHandler. When omitted, defaults to :byte-array.
(def resp
  (hop/send {:uri "http://www.example.com"}))

Read the response as a lookup map.

(println (:status resp)) ; 200
(println (:headers resp)) ; { some headers map value }
(println (:body resp)) ; Default body data as byte-array
(println (:media-type resp)) ; "text/html"
(println (:character-encoding resp)) ; "UTF-8"
(println (:content-type resp)) ; "text/html; charset=UTF-8"

Set Accept-Encoding Manually

Set accept-encoding manually.

(def resp
  (hop/send {:uri     "http://www.example.com"
             :headers {:accept-encoding "gzip"}}))

Decompress

(require '[dev.onionpancakes.hop.io :as io])

(slurp (io/decompress (:body resp) "gzip"))

License

Released under the MIT License.