JulianBirch / cljs-ajax

simple asynchronous Ajax client for ClojureScript and Clojure
671 stars 137 forks source link

Can't access response headers #266

Open njerig opened 3 years ago

njerig commented 3 years ago

How do I get the response headers from a raw response? I've tried looking at the headers object in the response XhrIo object, but its map_ and keys_ are empty.

udkl commented 2 years ago

You need to define a ResponseFormat that reads & returns what you want instead of just the formatted body returned by the defaults (json-response-format, text-response-format etc) :

(defn my-json-response-format [{:keys [prefix keywords? raw]}]
  (int/map->ResponseFormat
    {:read    (fn [xhrio]
                     {:body             (ajax.json/read-json-native raw keywords? (ajax.protocols/-body xhrio))
                      :status           (ajax.protocols/-status xhrio)
                      :status-text      (ajax.protocols/-status-text xhrio)
                      :response-headers (ajax.protocols/-get-all-headers xhrio)
                      :was-aborted      (ajax.protocols/-was-aborted xhrio)})
     :description  (str "JSON"
                        (if prefix (str " prefix '" prefix "'"))
                        (if keywords? " keywordize"))
     :content-type ["application/json"]}))