clj-http/get url {:as :json} doesn't work in script but in REPL

649 views Asked by At

I'm experimenting with Clojure and Leiningen. I was successful in executing to following line in the REPL:

(print (:body (client/get "https://coinbase.com/api/v1/prices/spot_rate?currency=CAD" {:as :json}))

I created a project with lein new http. When I run the following lines witn lein run then the coercion to JSON doesn't work. It simply prints a correct JSON string.

(ns http.core
  (:require [clj-http.client :as client])
  (:use clojure.pprint))

(defn -main
  []
  (print
    (:body
      (client/get "https://coinbase.com/api/v1/prices/spot_rate?currency=CAD" {:as :json}))

the output of the script is

{"amount":"306.89","currency":"CAD"}

Any idea what's wrong?

2

There are 2 answers

0
RedDeckWins On BEST ANSWER

I don't know exactly what changed, but [clj-http "1.1.2"] has the behavior you want.

0
Steffen Roller On

As it turned out there was a breaking change with clj-http version 2.0.0. Now one has to list explicitly the optional dependencies in project.clj. After I added

[cheshire "5.5.0"]

to my list of dependencies the program worked as expected. Please see the documentation for the change here.