Clojure contrib http-agent hangs when making a POST request

14 views
Skip to first unread message

Alex

unread,
Oct 29, 2009, 7:10:56 PM10/29/09
to Clojure
Hi,

I'm getting some strange errors when trying to make a POST request
using the Clojure contrib http-agent library (http://
richhickey.github.com/clojure-contrib/http.agent-api.html).

When I run:

(use 'clojure.contrib.http.agent)

(println (string (http-agent "http://www.google.com" :method
"POST" :body "param=true")))

The REPL simply hangs forever.

However, when I run the following:

(def agt (http-agent "http://www.google.com" :method "POST" :body
"param=true"))
;...wait a bit or add a (Thread/sleep 1000)
(println (string agt))

I get a correct response (Google saying it doesn't like POST requests)

Also, the (result ... ) function appears to work fine also:

(println (result (http-agent "http://www.google.com" :method
"POST" :body "param=true")))

So it looks like something in the (string ... ) function is causing it
to hang if the response has not yet completed. Anyone have any idea
what might be causing this?

Thanks,

Alex

Does anyone have any ideas why

Rob Wolfe

unread,
Oct 30, 2009, 5:58:28 PM10/30/09
to clo...@googlegroups.com
Alex <alexsp...@gmail.com> writes:

I guess the problem is in "string" function, because it tries
to get "content encoding" of stream which is not available.
In this case output stream was presumably closed by server.
So I can see two solutions:

1) always waiting until request is completed using "result" function

<code>
(ns test2
(:require [clojure.contrib.http.agent :as http]
[clojure.contrib.duck-streams :as ds]))

(let [agnt
(http/http-agent "http://www.google.com"
:method "POST"
:body "param=true")]
(http/result agnt)
(println "string: " (http/string agnt)))
(shutdown-agents)
</code>

2) applying this patch on original clojure.contrib.http.agent,
which imho solves this problem

<patch>
--- a/src/clojure/contrib/http/agent.clj
+++ b/src/clojure/contrib/http/agent.clj
@@ -263,9 +263,12 @@
headers, or clojure.contrib.duck-streams/*default-encoding* if it is
not specified."
([http-agnt]
- (string http-agnt (or (.getContentEncoding
- #^HttpURLConnection (::connection @http-agnt))
- duck/*default-encoding*)))
+ (let [a @http-agnt]
+ (if (= (::state a) ::receiving)
+ (string http-agnt (or (.getContentEncoding
+ #^HttpURLConnection (::connection @http-agnt))
+ duck/*default-encoding*))
+ (string http-agnt duck/*default-encoding*))))
([http-agnt #^String encoding]
(.toString (get-byte-buffer http-agnt) encoding)))
</patch>

HTH,
Rob

Alex

unread,
Oct 31, 2009, 7:00:56 AM10/31/09
to Clojure
Rob, that's perfect. Thanks very much for looking into that and
supplying the patch. Hopefully we can get that applied to the source
in git.

On Oct 30, 9:58 pm, Rob Wolfe <r...@smsnet.pl> wrote:

Stuart Sierra

unread,
Nov 2, 2009, 11:45:34 AM11/2/09
to Clojure
I've pushed a slightly different fix; (string...) now waits for the
HTTP request to finish.

-SS
Reply all
Reply to author
Forward
0 new messages