Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,19 @@
(assoc resp :body (String. ^"[B" body charset)))))

(defn coerce-clojure-body
[request {:keys [body] :as resp}]
[request {:keys [body status] :as resp}]
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")
body (util/force-byte-array body)]
body (util/force-byte-array body)
body-str (and (seq body) (String. ^"[B" body charset))]
(assoc resp :body (cond
(empty? body) nil
edn-enabled? (parse-edn (String. ^"[B" body charset))
edn-enabled? (if (unexceptional-status? status)
(parse-edn body-str)
(try
(parse-edn body-str)
(catch Exception _ body-str)))
:else (binding [*read-eval* false]
(read-string (String. ^"[B" body charset)))))))
(read-string body-str))))))

(defn coerce-transit-body
[{:keys [transit-opts coerce] :as request}
Expand Down
7 changes: 7 additions & 0 deletions test/clj_http/test/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,13 @@
(:body (client/coerce-response-body {:as :x-www-form-urlencoded}
www-form-urlencoded-resp))))))

(deftest t-coercion-method-error
(let [edn-body (ByteArrayInputStream. (.getBytes "error: not found"))
edn-resp {:body edn-body :status 404
:headers {"content-type" "application/edn"}}]
(is (= "error: not found"
(:body (client/coerce-response-body {:as :clojure} edn-resp))))))

(deftest ^:integration t-with-middleware
(run-server)
(is (:request-time (request {:uri "/get" :method :get})))
Expand Down