Search code examples
httpclojurerequestclojurescript

clojurescript http request returns empty


I'm trying to build a simple webapp using clojure/clojurescript for backend/frontend respectively. I'm having trouble establishing communication between the two of them via http requests, I feel like my issue is in the frontend but I'm not completly sure.

Here's my back end code:

  (ns example.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [clojure.data.json :as json]
            [clj-http.client :as client]
            [datomic.api :as d]
            [ring.util.response :as resp]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]])
  (:use [hiccup.core]))


  ;; Route
  (defroutes app-routes
    (GET "/info" [] (json/write-str {:data (some-function x)})))

So far so good, when I go to localhost:3000/info I see on the browser something like {"data":10} which is the expected result. I read that using cljs-http is the standard for clojurescript requests, so I followed the simple example on the repo and my code looks like this:

(ns example-cljs.core
  (:require [reagent.core :as reagent :refer [atom]]
            [clojure.core.async :as async]
            [cljs-http.client :as http]))

(def example-req (atom nil))

(defn get-data []
  (async/go
    (let [response (async/<! (http/get "localhost:3000/info"))]
      (reset! example-req response))))

But the response is just {:status 0, :success true, :body "", :headers {}, :trace-redirects ["localhost:3000/info" "localhost:3000/info"], :error-code :no-error, :error-text ""}

I feel like this is such a rookie mistake but I just can't find where it is.

Thanks for any help or hint.


Solution

  • So, after a some research I found to this tutorial so I'm just gonna copy-paste what I found that solved the issue:

    Add ring-cors dependency to hello-compojure in project.clj.

     :dependencies [[org.clojure/clojure "1.10.0"]
                     [org.clojure/tools.nrepl "0.2.13"]
                     [com.datomic/datomic-pro "0.9.6024"]
                     [org.clojure/data.json "0.2.6"]
                     [ring-cors "0.1.13"]
                     [compojure "1.6.1"]
                     [hiccup "1.0.5"]
                     [ring/ring-defaults "0.3.2"]]
    

    Require it in handler.clj in the server.

    (ns hello-compojure.handler
          (:require [compojure.core :refer :all]
                    [compojure.route :as route]
                    [datomic.api :as d]
                    [clojure.data.json :as json]
                    [ring.util.response :as resp]
                    [ring.middleware.cors :refer [wrap-cors]]
                    [ring.middleware.defaults :refer [wrap-defaults site-defaults]])
    

    And finally replace app definition in handler.clj with this :

    (def app
      (-> app-routes
          (wrap-cors :access-control-allow-origin [#".*"]
                     :access-control-allow-methods [:post :get]
                     :access-control-allow-credentials "true"
                     :access-control-allow-headers "Content-Type, Accept, Authorization, Authentication, If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since")
          (wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))))