Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow request bodies to be set.
  • Loading branch information
technomancy committed May 1, 2009
1 parent 5cad8b2 commit f565569
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/clojure/http/client.clj
Expand Up @@ -57,8 +57,8 @@ by a server."
cookie-map)))

(defn request
"Perform an HTTP request on url u."
[u & [method headers cookies]]
"Perform an HTTP request on url u. "
[u & [method headers cookies body]]
(let [connection (.openConnection (url u))
method (.toUpperCase (as-str (or method
"GET")))]
Expand All @@ -73,13 +73,25 @@ by a server."
(.setRequestProperty connection
"Cookie"
(create-cookie-string cookies)))
(.connect connection)
(if body
(do
(.setDoOutput connection true)
(.setRequestProperty connection
"Content-Type"
"application/x-www-form-urlencoded")
(.connect connection)
(.write (.getOutputStream connection)
(if (isa? body String)
body
(str-join "&" (map #(str-join "=" %) body)))))
(.connect connection))

(let [headers (parse-headers connection)]
{:body-seq (body-seq connection)
:code (.getResponseCode connection)
:msg (.getResponseMessage connection)
:headers (dissoc headers "Set-Cookie")
;; This correctly implements case-insensitive lookup.
:get-header #(.getHeaderField connection (as-str %))
:cookies (parse-cookies (get headers "Set-Cookie" nil))
:cookies (parse-cookies (headers "Set-Cookie"))
:url (str (.getURL connection))})))

0 comments on commit f565569

Please sign in to comment.