“I have my data ready to go. How do I get it to clojurescript?”

If you’ve been focused on backend work, it can be confusing when you get started on the frontend. One of the first steps is getting your data from the backend to the frontend.

cljs-http is a nice library that makes it simple to get your data and move on to what you are really trying to accomplish.

Let’s say you have the following JSON on your server, located at /resources/clojure-langs.json:

  [{"name": "clojure", "host-language": "java"},
   {"name": "clojurescript", "host-language": "javascript"},
   {"name": "clojure-clr", "host-language": "c#"}]}

Making a simple remote call to the server only takes a few lines:

(ns cljs-made-easy.remote-calls
  (:require [cljs-http.client :as http]
            [cljs.core.async :refer [<!]])
  (:require-macros [cljs.core.async.macros :refer [go]]))

(defn make-remote-call [endpoint]
  (go (let [response (<! (http/get endpoint))]
        ;;enjoy your data
        (js/console.log (:body response)))))

;;this could be a static file or an endpoint that generates the JSON
(make-remote-call "/resources/clojure-langs.json")

Here’s a live example, retrieving the static JSON from my server:

Simple get and post will get you a long way, and the docs provide great examples for the times you need more options.

Grow As A Professional Developer

I write about ClojureScript and what it means to be a professional.
Sign up to get posts directly in your inbox.

    I won't send you spam. Unsubscribe at any time.