Liking cljdoc? Tell your friends :D

cljs-sync-request

A ClojureScript wrapper around the NPM sync-request module

Note: cljs-sync-request does not bundle with sync-request

Latest Version

Clojars Project cljdoc badge CircleCI

Why?

Sometimes you just need to perform a synchronous HTTP requests without worrying about entering callback hell or propagating core.async go blocks and channels throughout your code. I use cljs-sync-request primarily in AWS Lambda functions where I don't need to do any asynchronous work and am typically wait for a result before continuing processing.

As the base sync-request library suggests, you are unlikely to want to use this library in production.

Example Usage

(ns io.axrs.cljs-sync-request.example
  (:require
    [io.axrs.cljs.sync-request.core :refer [wrap-sync-request]]
    ["sync-request" :as sync-request]])) ; When using shadow-cljs
    
(def transformers {"application/json" {:decode #(js->clj (js/JSON.parse %)) 
                                       :encode #(js/JSON.stringify (clj->js %))}})
(def request (wrap-sync-request sync-request transformers))

(defn check-status []
  (let [{:keys [status body headers] :as response} (request {:body {:id "123"} 
                                                             :content-type "application/json" 
                                                             :url "http://localhost"
                                                             :method "POST"})
    (if (= 200 status)
      (continue-processing body)
      (handle-other-codes response))))

Can you improve this documentation? These fine people already did:
Alexander Scott & Alexander R Scott
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close