Alex <alexspurl...@gmail.com> writes:

> 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?

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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to