Cors issue

Given a solo topology running a rest API gateway with a REST protocol that which connects to a handler that returns the correct headers to enable cors. e.g

(def cors-headers {"Access-Control-Allow-Origin"  "*"
                   "Access-Control-Allow-Methods" "GET, PUT, PATCH, POST, DELETE, OPTIONS"
                   "Access-Control-Allow-Headers" "Authorization, Content-Type"})

(defn edn-response
  {:status  200
   :headers (merge cors-headers
                   {"Content-Type" "application/edn"})
   :body    body})

(defn get-items-by-type
  "Web handler that returns info about items matching type."
  [{:keys [headers body]}]
  (let [type (some-> body edn/read)]
    (if (keyword? type)
      (-> (starter/get-db)
          (starter/get-items-by-type type [:inv/sku :inv/size :inv/color])
      {:status  400
       :headers {}
       :body    "Expected a request body keyword naming a type"})))

(def get-items-by-type-lambda-proxy
  (apigw/ionize get-items-by-type))
i would expect a fetch call from my local browser e.g 

(js/fetch ""
          {:mode    "cors"
           :method  "POST"
           :headers {"Content-Type" "text/plain"}
           :body    ":hat"

To succeed, instead, I get a cors error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Which I’m not sure how to resolve give I have added it to the handler.

I have also followed the instructions here: Enabling CORES in a Lambda Proxy | Datomic

though I myself didn’t set setup cogito. My API end point is reachable by curl. I also enabled cors via API gateway > apis > (select API) > actions > enable cors

I read the other forum issues on cors to. thanks for any help you can give.

Further conversation in slack revealed that when the end point was curled the expected headers were being returned. Then upon closer inspection the issue was with the js fetch call. The second argument needs to be a javascript object and not a map clj->js

1 Like