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

ring-respones-format returns a nil result on 204 no content

Open feral-dot-io opened this issue 4 years ago • 3 comments

When the server responds with no content, while using ring-response-format, a nil result is passed to :handler instead of a map with :status, :headers, :body keys. This happens when talking to a REST API with responses that return 204 no content.

Here is an example:

(defn test-code [code]
  (ajax-request
   {:method :get
    :uri (str "https://httpbin.org/status/" code)
    :format (ajax/text-request-format)
    :response-format (ajax/ring-response-format
                      {:format (ajax/text-response-format)})
    :handler (fn [result]
               (println "Result:" result))}))

(test-code 204)
;; => Result: [true nil]
(test-code 201)
;; => Result: [true {:status 201, :headers {Date Sat, 24 Oct 2020 10:16:44 GMT, Content-Type text/html; charset=utf-8, Content-Length 0, Connection keep-alive, Server gunicorn/19.9.0, Access-Control-Allow-Origin *, Access-Control-Allow-Credentials true}, :body }]

The distinguishing factor seems to be that the server sends a "Content-Lengh: 0" for 201 and nothing for 204:

[joshua@pomelo:~]$ curl -v https://httpbin.org/status/204 2>&1 | grep content-length -i
[joshua@pomelo:~]$ curl -v https://httpbin.org/status/201 2>&1 | grep content-length -i
< content-length: 0

I think conceptually they should probably be treated the same giving a zero-length :body for both.

feral-dot-io avatar Oct 24 '20 10:10 feral-dot-io

You're right, that's a bug. I'd appreciate a PR with a unit test.

JulianBirch avatar Feb 18 '21 12:02 JulianBirch

Hey! I've recently run into the same issue while using day8.re-frame/http-fx and I believe the culprit is this line in ajax.interceptors. I guess if they could return the Xhrio object without the body that would solve it. Something like the following, perhaps?

-          204 [true nil]       ; 204 and 205 should have empty responses
+          204 [true (dissoc xhrio :body)] ; 204 and 205 should have empty bodies
-          205 [true nil]
+          205 [true (dissoc xhrio :body)]

bigodel avatar Nov 23 '23 04:11 bigodel