Fork me on GitHub
#re-frame
<
2023-04-24
>
Benjamin15:04:23

Did you ever use http_fx and then wanted to access the response headers? Because I do right now.

p-himik15:04:40

There most likely is a way to access them by specifying some custom response format. The implementation of the underlying cljs-ajax is hard to follow due to the heavy reliance on protocols and so can't tell for certain how to do it without spending too much time on it. However, there's a different library that relies on js/fetch that also has that functionality built-in: https://github.com/superstructor/re-frame-fetch-fx#success-handling

👍 2
valtteri16:04:48

With cljs-ajax you can use a response-format like

{:content-type "something"     
 :description  "raw"
 :read         identity}
:read is a function that that manipulates the original response object where you can get the headers. If I recall, you can use these protocol methods to work with the response object https://github.com/JulianBirch/cljs-ajax/blob/master/src/ajax/protocols.cljc#L15-L28

valtteri16:04:38

And I agree with ☝️ that this was not trivial to figure out from the docs or from the source code.

Franklin09:05:58

Hey @U02CV2P4J6S! Did you figure this out? I did this a few months ago by adding the following to :http-xhrio effect

:response-format
                    {:read (fn [xhrio]
                             {:status (ajax.protocols/-status xhrio),
                              :headers (ajax.protocols/-get-all-headers
                                         xhrio)})},
So, in the on-success or on-failure handler, I desctured the response as follows:
{{:keys [status headers]} :response}