Skip to content

Commit

Permalink
add a :mime-subtype request parameter to override the multipart/... t…
Browse files Browse the repository at this point in the history
…ype in multipart requests
  • Loading branch information
pkalliok committed Jan 9, 2018
1 parent 3109f94 commit fae31a7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/clj_http/core.clj
Expand Up @@ -423,7 +423,7 @@
(.setCredentials authscope creds)))))
(if multipart
(.setEntity ^HttpEntityEnclosingRequest http-req
(mp/create-multipart-entity multipart))
(mp/create-multipart-entity multipart req))
(when (and body (instance? HttpEntityEnclosingRequest http-req))
(if (instance? HttpEntity body)
(.setEntity ^HttpEntityEnclosingRequest http-req body)
Expand Down
2 changes: 1 addition & 1 deletion src/clj_http/core_old.clj
Expand Up @@ -285,7 +285,7 @@
(.addHeader http-req header-n (str header-v))))
(if multipart
(.setEntity ^HttpEntityEnclosingRequest http-req
(mp/create-multipart-entity multipart))
(mp/create-multipart-entity multipart req))
(when (and body (instance? HttpEntityEnclosingRequest http-req))
(if (instance? HttpEntity body)
(.setEntity ^HttpEntityEnclosingRequest http-req body)
Expand Down
13 changes: 7 additions & 6 deletions src/clj_http/multipart.clj
Expand Up @@ -2,7 +2,7 @@
"Namespace used for clj-http to create multipart entities and bodies."
(:import (java.io File InputStream)
(org.apache.http.entity ContentType)
(org.apache.http.entity.mime MultipartEntity)
(org.apache.http.entity.mime MultipartEntityBuilder)
(org.apache.http.entity.mime HttpMultipartMode)
(org.apache.http.entity.mime.content ContentBody
ByteArrayBody
Expand Down Expand Up @@ -128,12 +128,13 @@
(defn create-multipart-entity
"Takes a multipart vector of maps and creates a MultipartEntity with each
map added as a part, depending on the type of content."
[multipart]
(let [mp-entity (MultipartEntity. HttpMultipartMode/STRICT
nil
(encoding-to-charset "UTF-8"))]
[multipart {:keys [mime-subtype]}]
(let [mp-entity (doto (MultipartEntityBuilder/create)
(.setStrictMode)
(.setCharset (encoding-to-charset "UTF-8"))
(.setMimeSubtype (or mime-subtype "form-data")))]
(doseq [m multipart]
(let [name (or (:part-name m) (:name m))
part (make-multipart-body m)]
(.addPart mp-entity name part)))
mp-entity))
(.build mp-entity)))

0 comments on commit fae31a7

Please sign in to comment.