r0man / cljs-http

A ClojureScript HTTP library.
582 stars 93 forks source link

Why would the cljs-http.client request be blocked by the cors policy if the clj-http.client request isn't? #127

Open zendevil opened 4 years ago

zendevil commented 4 years ago

I make a request like so using clj-http.client

(http/get "https://www.example.com/bar")

and get a response with status 200.

However, when I make the same request with cljs-http.client like so:

(go (let [response (<! (http/get (str "https://www.example.com" "/bar")))]
         (prn response)))

I get a response with status 0, and in the browser it says that it's been blocked by the cors policy.

Why would the cljs-http.client request be blocked by the cors policy if the clj-http.client request isn't? I have manually set the headers in ring like so:

(defn response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/edn"
             "Access-Control-Allow-Headers" "Content-Type"
             "Access-Control-Allow-Origin" "*"
             "Access-Control-Request-Method" "GET"}
   :body (pr-str data)})

I wrap my response for route "bar" with the function response above.

victorb commented 4 years ago

Any chance you're calling clj-http.client from a Clojure context (instead of ClojureScript)? AFAIK, clj-http only works in Clojure, so it's running server-side, where there is no CORS restrictions, only the browser requests has to follow the rules of CORS.