cch1 / http.async.client

Async Http Client - Clojure
http://cch1.github.com/http.async.client
267 stars 40 forks source link

Every request fails due to java.net.ConnectException #44

Closed aperiodic closed 11 years ago

aperiodic commented 11 years ago

Everything I try to request fails. I'm using version 0.5.0, and following the example usage from the docs:

pluck.main=> (with-open [client (http/create-client)] (http/GET client "http://google.com"))
{:id req-id__7105, :url "http://google.com", :raw-url "http://google.com", :status #<core$promise$reify__6153@106305aa: :pending>, :headers #<core$promise$reify__6153@5508e44d: :pending>, :body #<core$promise$reify__6153@3ac4a6d8: :pending>, :done #<core$promise$reify__6153@44a5cd0c: true>, :error #<core$promise$reify__6153@6138f209: #<ConnectException java.net.ConnectException: http://google.com/>>} 
pluck.main=> (http/error *1)
#<ConnectException java.net.ConnectException: http://google.com/>

This happens both from a swank server and a lein 2 repl. In both cases, the clj-http client works fine. This is using clojure 1.4.0 on OS X 10.6.8.

neotyk commented 11 years ago

Hello Dan,

Please point me to documentation that mentioned this example, since it is wrong in two places.

To fix it you need to:

  1. take into account asynchrony of request, in example you provided client is closed straight after request is created, You should (http/await request) before closing client.
  2. Google replies with status code 301 and you probably want to fetch content, that can be configured at client creation.

So example should look like so:

(with-open [c1 (http/create-client :follow-redirects true)]
   (http/await
    (http/GET c1 "http://google.com")))

It is not very efficient to create client per each request, better would be to have client living longer that request.

Thank you for your feedback!

aperiodic commented 11 years ago

Augh, I skimmed right into the invocations in the detailed start section and didn't realize why await was important. My mistake. Thanks for the pointer!