JulianBirch / cljs-ajax

simple asynchronous Ajax client for ClojureScript and Clojure
670 stars 136 forks source link

Custom header params #233

Closed ILoveHubGit closed 5 years ago

ILoveHubGit commented 5 years ago

I'm trying to call an API that requests a custom header parameter, but I don't know how to add this. If I'm using this:

{:http-xhrio {:method          :get
              :uri             (str baseuri "/resource/" 1 "/subres/" 2)
              :headers         {"Accept" "application/json"}
              :keywords?       true
              :response-format (http/json-response-format {:keywords? true})

I see a header parameter: Accept: application/json But if I try this

{:http-xhrio {:method          :get
              :uri             (str baseuri "/resource/" 1 "/subres/" 2)
              :headers         {"Accept" "application/json" "Custom" "Value"}
              :keywords?       true
              :response-format (http/json-response-format {:keywords? true})

I get: Access-Control-Request-Headers: Custom

Instead of:

Accept: application/json
Custom: Value

The value does not appear and the server can't answer my request due to malformed request.

What am I doing wrong?

JulianBirch commented 5 years ago

Hi, sorry it's taken so long to reply. You shouldn't need to set the Accept header because the response format should do that for you. (And that might be what's happening with the first example.) Other than that.. I'm afraid I'm not sure. I'm not sure what method you're using as well: are you using POST or ajax-request?

ILoveHubGit commented 5 years ago

Sorry from my side now for answering so late. The whole problem I had here had noting to do with cljs-ajax, this library is working fine. The problem was that the server I called did not answer correct on the OPTIONS call made by the browser. This problem is related to CORS (Cross Origin Resource Sharing). On top of this the browser I used (Brave) only handles CORS calls correct if you allow third party cookies. This has been reported as a bug at Brave. Thanks for the reply.