Skip to content

Commit

Permalink
WIP for multipart upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Nov 7, 2011
1 parent 9c18043 commit 411b755
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions project.clj
Expand Up @@ -2,6 +2,7 @@
:description "A Clojure HTTP library wrapping the Apache HttpComponents client."
:dependencies [[org.clojure/clojure "1.3.0"]
[org.apache.httpcomponents/httpclient "4.1.2"]
[org.apache.httpcomponents/httpmime "4.1.2"]
[commons-codec "1.5"]
[commons-io "1.4"]
[slingshot "0.8.0"]]
Expand Down
14 changes: 10 additions & 4 deletions src/clj_http/core.clj
Expand Up @@ -6,6 +6,7 @@
HttpResponse Header HttpHost)
(org.apache.http.util EntityUtils)
(org.apache.http.entity ByteArrayEntity)
(org.apache.http.entity.mime MultipartEntity)
(org.apache.http.client HttpClient)
(org.apache.http.client.methods HttpGet HttpHead HttpPut
HttpPost HttpDelete
Expand Down Expand Up @@ -56,7 +57,7 @@
the clj-http uses ByteArrays for the bodies."
[{:keys [request-method scheme server-name server-port uri query-string
headers content-type character-encoding body socket-timeout
conn-timeout debug insecure?] :as req}]
conn-timeout multipart debug insecure?] :as req}]
(let [http-client (DefaultHttpClient. (connection-manager insecure?))]
(try
(doto http-client
Expand Down Expand Up @@ -92,9 +93,14 @@
(.addHeader http-req "Connection" "close")
(doseq [[header-n header-v] headers]
(.addHeader http-req header-n header-v))
(when body
(let [http-body (ByteArrayEntity. body)]
(.setEntity #^HttpEntityEnclosingRequest http-req http-body)))
(if multipart
(let [mp-entity (MultipartEntity.)]
(doseq [[k v] multipart]
(.addPart mp-entity (name k) v))
(.setEntity #^HttpEntityEnclosingRequest http-req mp-entity))
(when body
(let [http-body (ByteArrayEntity. body)]
(.setEntity #^HttpEntityEnclosingRequest http-req http-body))))
(when debug
(println "Request:")
(clojure.pprint/pprint req)
Expand Down

0 comments on commit 411b755

Please sign in to comment.