Skip to content

Commit 2173932

Browse files
saitouenadakrone
authored andcommitted
Add option to add multipart charset (#522)
* add option to add multipart charset see also: https://stackoverflow.com/questions/3393445/international-characters-in-filename-in-mutipart-formdata To handle non-ascii characters, we need a way to set .setCharset and .setMode BROWSER_COMPATIBLE
1 parent ff91bc6 commit 2173932

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

README.org

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ content encodings.
383383
:retry-handler (fn [ex try-count http-context]
384384
(println "Got:" ex)
385385
(if (> try-count 4) false true))})
386+
387+
;; to handle a file with non-ascii filename, try :multipart-charset "UTF-8" and :multipart-mode BROWSER_COMPATIBLE
388+
;; see also: https://stackoverflow.com/questions/3393445/international-characters-in-filename-in-mutipart-formdata
389+
(import (org.apache.http.entity.mime HttpMultipartMode))
390+
391+
(client/post "http://example.org" {:multipart [{:content (clojure.java.io/file "日本語.txt")}]
392+
:multipart-mode HttpMultipartMode/BROWSER_COMPATIBLE
393+
:multipart-charset "UTF-8"} )
394+
386395
#+END_SRC
387396

388397
A word about flattening nested =:query-params= and =:form-params= maps. There are essentially three

src/clj_http/core.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@
572572
uri response-interceptor proxy-host proxy-port
573573
http-client-context http-request-config http-client
574574
proxy-ignore-hosts proxy-user proxy-pass digest-auth ntlm-auth
575-
multipart-mode
575+
multipart-mode multipart-charset
576576
; deprecated
577577
conn-timeout conn-request-timeout]
578578
:as req} respond raise]

src/clj_http/multipart.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@
128128
(defn create-multipart-entity
129129
"Takes a multipart vector of maps and creates a MultipartEntity with each
130130
map added as a part, depending on the type of content."
131-
[multipart {:keys [mime-subtype multipart-mode]
131+
[multipart {:keys [mime-subtype multipart-mode multipart-charset]
132132
:or {mime-subtype "form-data"
133133
multipart-mode HttpMultipartMode/STRICT}}]
134134
(let [mp-entity (doto (MultipartEntityBuilder/create)
135135
(.setMode multipart-mode)
136136
(.setMimeSubtype mime-subtype))]
137+
(when multipart-charset
138+
(.setCharset mp-entity (encoding-to-charset multipart-charset)))
137139
(doseq [m multipart]
138140
(let [name (or (:part-name m) (:name m))
139141
part (make-multipart-body m)]

test/clj_http/test/multipart_test.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
(is (= "testname" (.getFilename body)))))))
177177

178178
(deftest test-multipart-content-charset
179-
(testing "charset is nil for all multipart requests"
179+
(testing "charset is nil if no multipart-charset is supplied"
180180
(let [mp-entity (create-multipart-entity [] nil)]
181-
(is (nil? (EntityUtils/getContentCharSet mp-entity))))))
181+
(is (nil? (EntityUtils/getContentCharSet mp-entity)))))
182+
(testing "charset is set if a multipart-charset is supplied"
183+
(let [mp-entity (create-multipart-entity [] {:multipart-charset "UTF-8"})]
184+
(is (= "UTF-8" (EntityUtils/getContentCharSet mp-entity))))))

0 commit comments

Comments
 (0)