Fork me on GitHub
#reitit
<
2021-04-23
>
grounded_sage10:04:41

Is there a simple API first reitit demo. Curious as I am having trouble getting past cors issue. Not sure what I am missing.

grounded_sage10:04:22

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"})})))

emil0r10:04:10

If you're using cljs-ajax you need withCredentials and corresponding middleware in the backend

emil0r11:04:07

And you need :options for CORS iirc for the preflight check from the browser

grounded_sage11:04:53

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.

emil0r14:04:16

I can put together something later if no one else beats me to it

👍 3
emil0r14:04:11

See if you can move forward by adding :options to the CORS middleware

grounded_sage14:04:54

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.

ikitommi14:04:03

PR welcome.

ikitommi14:04:07

would love to see cors, security headers etc. as builtins. 100% busy atm, so, contributions welcome.

miikka14:04:18

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

🎉 9
miikka15:04:03

What's the CORS issue you're having?

grounded_sage15:04:04

Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

miikka15:04:56

Ok, let me quickly try this out

miikka15:04:45

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(' {
    return resp.text();
}).then(function(body) {
    console.log("body text:", body);
});

miikka15:04:15

Looking at the browser developer tools, the response has the CORS headers it should have (`Access-Control-Allow-Origin: http://localhost:8000` etc)

grounded_sage15:04:13

No idea why I can't get it working then

miikka15:04:50

https://github.com/miikka/reitit-cors-test here's the code i used. I mean, it just the code you posted, but still.

miikka15:04:02

Hmm, right, is it a GET request or something else that you're doing?

grounded_sage15:04:08

Yes was a GET request

miikka15:04:19

Ok. That should be the simplest case

miikka15:04:32

Does the response from the server look otherwise okay? Like the status code, response body, tec?

grounded_sage15:04:55

{:status 0, :success false, :body "", :headers {}, :trace-redirects ["" ""], :error-code :http-error, :error-text " [0]"}

miikka15:04:28

Right, I meant if you look at it in the browser debugger

miikka15:04:51

Network tab in Firefox developer tools

miikka15:04:05

Well, it's called Network in Chrome as well

grounded_sage15:04:28

Has a CORS error

miikka15:04:52

Right, yeah

miikka15:04:12

The debugger does not actually show that stuff when there's CORS error. Ah well.

grounded_sage15:04:58

Could it possibly be from the frontend doing something wrong?

grounded_sage15:04:26

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 ""))]
      (prn response)
      (prn (:status response))
      (prn (:body response))))

miikka15:04:41

Not sure, I think cljs-http should just work with the default settings?

miikka15:04:49

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)

grounded_sage15:04:02

haha the copy as curl works fine :man-shrugging::skin-tone-2:

miikka15:04:50

Hmm, I wonder if (http/get ""{:with-credentials? false}) would help

👏 3
grounded_sage15:04:16

I think there is something wrong with this client side http library

miikka15:04:00

Could be. I've successfully used it in CORS context previously though

miikka15:04:20

Ah well. I have to go now. I hope you'll figure it out!

grounded_sage15:04:17

That was it. Thanks! Super annoying. Thank you for helping me spot that!