From f5151223a605592afbbef6a9e6c1b7166ad13194 Mon Sep 17 00:00:00 2001 From: Stanislav Yurin Date: Tue, 16 Sep 2014 08:17:56 +0300 Subject: [PATCH 1/2] Added .setResponseType handling, raw :response and :response-type in response map --- src/cljs_http/core.cljs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cljs_http/core.cljs b/src/cljs_http/core.cljs index f93091c..15118d7 100644 --- a/src/cljs_http/core.cljs +++ b/src/cljs_http/core.cljs @@ -17,7 +17,8 @@ (defn request "Execute the HTTP request corresponding to the given Ring request map and return a core.async channel." - [{:keys [request-method headers body with-credentials?] :as request}] + [{:keys [request-method headers body response-type with-credentials?] :as request + :or {response-type ""}}] (let [channel (async/chan) request-url (util/build-url request) method (name (or request-method :get)) @@ -28,13 +29,16 @@ with-credentials?) xhr (doto (XhrIo.) (.setTimeoutInterval timeout) - (.setWithCredentials send-credentials))] + (.setWithCredentials send-credentials) + (.setResponseType response-type))] (swap! pending-requests assoc channel xhr) (.listen xhr EventType.COMPLETE #(let [target (.-target %1)] (->> {:status (.getStatus target) :success (.isSuccess target) :body (.getResponseText target) + :response-type (.getResponseType target) + :response (.getResponse target) :headers (util/parse-headers (.getAllResponseHeaders target)) :trace-redirects [request-url (.getLastUri target)]} (async/put! channel)) From 404b8b729ef657ff4a331ac07b8f2e538ef4e8ef Mon Sep 17 00:00:00 2001 From: Stanislav Yurin Date: Wed, 3 Jun 2015 19:39:15 +0300 Subject: [PATCH 2/2] Add and handle response-type parameter --- .gitignore | 3 +++ src/cljs_http/core.cljs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5a8f0b6..1cb33fa 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ /pom.xml.asc /.repl /.nrepl-port +*.iml +/.idea +/.cljs_rhino_repl diff --git a/src/cljs_http/core.cljs b/src/cljs_http/core.cljs index 201b703..63b5b5e 100644 --- a/src/cljs_http/core.cljs +++ b/src/cljs_http/core.cljs @@ -1,6 +1,7 @@ (ns cljs-http.core (:import [goog.net EventType ErrorCode XhrIo] - [goog.net Jsonp]) + [goog.net Jsonp] + [goog.net.XhrIo ResponseType]) (:require-macros [cljs.core.async.macros :refer [go]]) (:require [cljs-http.util :as util] [cljs.core.async :as async] @@ -31,7 +32,8 @@ (defn build-xhr "Builds an XhrIo object from the request parameters." - [{:keys [with-credentials? default-headers] :as request}] + [{:keys [with-credentials? response-type default-headers] :as request + :or {response-type goog.net.XhrIo.ResponseType.DEFAULT}}] (let [timeout (or (:timeout request) 0) send-credentials (if (nil? with-credentials?) true @@ -39,7 +41,8 @@ (doto (XhrIo.) (apply-default-headers! default-headers) (.setTimeoutInterval timeout) - (.setWithCredentials send-credentials)))) + (.setWithCredentials send-credentials) + (.setResponseType response-type)))) ;; Reverses the goog.net.ErrorCode constants to map to CLJS keywords (def error-kw @@ -55,7 +58,7 @@ (defn xhr "Execute the HTTP request corresponding to the given Ring request map and return a core.async channel." - [{:keys [request-method headers body with-credentials? cancel] :as request}] + [{:keys [request-method headers body response-type with-credentials? cancel] :as request}] (let [channel (async/chan) request-url (util/build-url request) method (name (or request-method :get)) @@ -67,7 +70,9 @@ (let [target (.-target evt) response {:status (.getStatus target) :success (.isSuccess target) - :body (.getResponseText target) + :body (if response-type + (.getResponse target) + (.getResponseText target)) :headers (util/parse-headers (.getAllResponseHeaders target)) :trace-redirects [request-url (.getLastUri target)] :error-code (error-kw (.getLastErrorCode target))