Skip to content

Addressing a problem with 404 errors for EDN requests #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
@@ -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}
7 changes: 7 additions & 0 deletions test/clj_http/test/client_test.clj
Original file line number Diff line number Diff line change
@@ -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})))