Search code examples
apihttp-headersclojurescript

How can I add custom header parameters to a API GET call in clojurescript


I'm building a demo application in clojurescript with KeeFrame and to retrieve a part of the information for this website I need to call an external API which requires a custom HTTP header parameter in the GET requests

I'm using re-frame.core for the API calls, which uses ajax.core. I also tried to replace this with cljs-http.client. However the result is the same. I already managed to add custom header parameters to the request header by using clj-http at server site. But this is not a solution I want to implement for this website because that means that I first have to rebuild the API I'm calling. So I can use it from my clojurescript without the parameter.

This code works. A correct GET request is generated

{:http-xhrio {
 :method          :get
 :uri             (str transuri "/acquirer/" 673072009 "/acquirerref/" acquirerRefNo)
 :headers         {"Accept" "application/json"}
 :response-format (http/json-response-format {:keywords? true})
 :on-failure      [:common/set-error]}}

With "Accept: application/json" as a request header

This code does not work. Instead of a GET request an OPTIONS request is generated

{:http-xhrio {
 :method          :get
 :uri             (str transuri "/acquirer/" 673072009 "/acquirerref/" acquirerRefNo)
 :headers         {"Accept" "application/json" "Custom" "Value"}
 :response-format (http/json-response-format {:keywords? true})
 :on-failure      [:common/set-error]}}

And in the request header "Accept: application/json" is not visible but "Access-Control-Request-Headers: custom" is

I expected a GET request with "Accept: application/json" and "Custom: Value" in the request header.

Can someone tell me what I'm doing wrong or provide me with a link with information about this?

Thanks in advance


Solution

  • The Browser will send a "preflight" OPTIONS request to verify that it is allowed to send the "Custom" request header. The server is supposed to approve by replying with "Access-Control-Allow-Headers".

    See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS