开发者

Get body of HTTP response in Clojure

开发者 https://www.devze.com 2023-04-10 05:29 出处:网络
I\'m trying to get the body of a HTTP response with Clojure, with a handler. However the http-agent function hangs without returning.

I'm trying to get the body of a HTTP response with Clojure, with a handler. However the http-agent function hangs without returning.

This will print the response, and then hang without returning:

(use '[clojure.contrib.http.agent])

(def text (result (http-agent "http://jsonip.com"
                   :method "GET")))
(println text)

This will print "Handling...", then hang indefinitely:

(use '[clojure.contrib.http.agent])

(defn do-stuff
  "handler"
  [response]
  (do 
    (println "Handling...")
    (slurp (string response))))

(def text (result (http-agent "http://jsonip.com"
  :method "GET"开发者_如何学Go
  :handler do-stuff)))

(println (str "text! " text))

How can I get the http-agent method to stop hanging? In the second case I've listed above, how can I get the handler to return the response body?

Thanks for your help, Kevin


In the second piece of code you have not printed out what is slurped. Should be like this -

(println (slurp (string response)))
0

精彩评论

暂无评论...
验证码 换一张
取 消