Is there a simple API first reitit demo. Curious as I am having trouble getting past cors issue. Not sure what I am missing.
What am I missing here?
(defn handler [_]
{:status 200, :body "hello app"})
(def app
(ring/ring-handler
(ring/router
["/" {:get handler}]
{:exception pretty/exception
:data {:middleware [[wrap-cors :access-control-allow-origin [#".*"]
:access-control-allow-methods [:get :put :post :patch :delete]]]}})
(ring/create-default-handler {:not-found (constantly {:status 404 :body "Not found"})})))
If you're using cljs-ajax you need withCredentials and corresponding middleware in the backend
And you need :options for CORS iirc for the preflight check from the browser
using cljs-http on the frontend. This answer still confuses me. Would be easier if I could see a code example. All ones I have seen in the wild do not work for me.
I can put together something later if no one else beats me to it
See if you can move forward by adding :options to the CORS middleware
I find it strange how I've found like 5 different examples on how to handle CORS. Hard to tell which one is the more idiomatic way to do it in reitit. Given this is like a fundamental piece I would think there would be documentation right there on the library. Even if it is until a more bundled approach is achieved.
PR welcome.
would love to see cors, security headers etc. as builtins. 100% busy atm, so, contributions welcome.
Just released reitit 0.5.13. A minor fix to create-resource-handler/create-file-handler and some doc and deps updates. https://github.com/metosin/reitit/blob/master/CHANGELOG.md#0513-2021-04-23
I've tried:
The interceptor way with this library: https://github.com/zerg000000/simple-cors
The {:data ...}
pattern found here: https://github.com/prestancedesign/todo-backend-clojure-reitit/blob/master/src/todo_backend/core.clj
This example here: https://github.com/metosin/reitit/issues/143#issuecomment-421781636
The example here (which uses options). https://clojurians-log.clojureverse.org/reitit/2020-05-10
What's the CORS issue you're having?
Access to XMLHttpRequest at '<http://localhost:9090/>' from origin '<http://localhost:3000>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
Ok, let me quickly try this out
Hmh, I wonder what is going on. I tried the code with wrap-cors you pasted above and the following JavaScript served from a separate port and it seemed to work just fine:
fetch('<http://localhost:3000/').then(function(resp)> {
return resp.text();
}).then(function(body) {
console.log("body text:", body);
});
Looking at the browser developer tools, the response has the CORS headers it should have (`Access-Control-Allow-Origin: http://localhost:8000` etc)
weird
No idea why I can't get it working then
https://github.com/miikka/reitit-cors-test here's the code i used. I mean, it just the code you posted, but still.
Hmm, right, is it a GET request or something else that you're doing?
Yes was a GET request
Ok. That should be the simplest case
Does the response from the server look otherwise okay? Like the status code, response body, tec?
{:status 0, :success false, :body "", :headers {}, :trace-redirects ["<http://localhost:9000>" "<http://localhost:9000>"], :error-code :http-error, :error-text " [0]"}
Right, I meant if you look at it in the browser debugger
Network tab in Firefox developer tools
Well, it's called Network in Chrome as well
Has a CORS error
Right, yeah
The debugger does not actually show that stuff when there's CORS error. Ah well.
Could it possibly be from the frontend doing something wrong?
This is basically what I have atm. Obviously port being what the port is for server.
(ns app.core
(:require [cljs-http.client :as http]
[cljs.core.async :refer [go <!]]))
(go (let [response (<! (http/get "<http://localhost:9000>"))]
(prn response)
(prn (:status response))
(prn (:body response))))
Not sure, I think cljs-http should just work with the default settings?
I'm starting to run out of ideas. You could try to run the same request with curl and check that the output looks right, but otherwise I don't know what to do. (You can right-click that request in Chrome Network tab and select Copy -> Copy as cURL to get the ready-to-run command-line command)
haha the copy as curl works fine :man-shrugging::skin-tone-2:
Hmm, I wonder if (http/get "<http://localhost:9000/>"{:with-credentials? false})
would help
Iย think there is something wrong with this client side http library
Could be. I've successfully used it in CORS context previously though
Ah well. I have to go now. I hope you'll figure it out!
That was it. Thanks! Super annoying. Thank you for helping me spot that!