cljs-ajax icon indicating copy to clipboard operation
cljs-ajax copied to clipboard

Can't access response headers

Open njerig opened this issue 3 years ago • 1 comments

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.

njerig avatar Jun 11 '21 01:06 njerig

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"]}))

udkl avatar Sep 03 '22 16:09 udkl