clj-http icon indicating copy to clipboard operation
clj-http copied to clipboard

Do I need to close the stream in case of exception?

Open igrishaev opened this issue 6 years ago • 2 comments

According to the docs, when I receive a stream from the server, the connection hangs until I consume the whole stream:

;; Return the body as a stream
(client/get "http://example.com/bigrequest.html" {:as :stream})
;; Note that the connection to the server will NOT be closed until the
;; stream has been read

My question is, what will happen in case of non-200 exception? Do I still need to close the stream properly? I added the following code:

(try
  (clj-http.client/get "http://non-existing-url" {:as :stream})
  (catch Exception e
    (some-> e ex-data :body .close)))

But I'm still not sure if I need that.

igrishaev avatar Aug 13 '18 11:08 igrishaev

+1

torkus avatar Mar 28 '19 01:03 torkus

Hi @igrishaev, clj-http doesn't do anything with the stream in the event that is a non-200 exception. In the event of a non-200 exception a user may still want to read from the stream (to get the body of the error for instance), so it doesn't automatically close.

Would you be willing to add something to the documentation about that?

dakrone avatar Apr 30 '19 15:04 dakrone