How to send a file to a POST request?

I am creating a clojure API for my website, which is basically a wrapper around the original web API. One of the features that I cannot realize is sending files via POST requests, basically what I would do in a shell with curl -F foo=bar baz=@bak.jpg foobar.com.

I use clojure-http-client and initially tried the form (resourcefully/post "foobar.com" {} {:foo "bar" :baz (File. "bak.jpg")}), but the: baz field was ignored by the script, as if I sent only: foo. Later I tried to change File.to FileInputStream, because [line 51] [2] client.clj seems to check this class, but still got the same results.

Then I created a php page that just prints $ _POST to check my request, and it seems that object data is sent literally. Take a look:

Clojure => (resourceful / post " http://ptchan.org/pttest.php " {} {: foo "bar": baz "/tmp/bak.jpg"}) {: body-seq ("Array" " ("" [foo] => bar "" [baz] => /tmp/bak.jpg "") "): code 200 ,: msg" OK ",: method" POST ",: headers {: date ( "Fri, Oct 02, 2009 11:41:15 GMT") ,: various ("Accept-Encoding") ,: content length ("53") ,: connection ("close" ") ,: content-type (" text / html "): server (" Apache / 2.2.9 (Debian) PHP / 5.2.6-1 + lenny3 with Suhosin-Patch "): x-powered by by (" PHP / 5.2.6-1 + lenny3 ")} ,: get-header # ,: cookies nil ,: url" http://ptchan.org/pttest.php "}

Clojure => (resourcefully / post " http://ptchan.org/pttest.php " {} {: foo "bar": baz (File. "/Tmp/bak.jpg")}) {: body-seq ( "Array" "(" "[foo] => bar" "[baz] => /tmp/bak.jpg" ")") ,: code 200 ,: msg "OK" ,: method "POST" ,: headers {: date ("Fri, 02 Oct 2009 11:41:30 GMT"),: various ("Accept-Encoding") ,: content length ("53") ,: connection ("close" "),: content- type ("text / html") ,: server ("Apache / 2.2.9 (Debian) PHP / 5.2.6-1 + lenny3 with Suhosin-Patch"): x-powered by by ("PHP / 5.2.6 -1 + lenny3 ")} ,: get-header # ,: cookies nil ,: url" http://ptchan.org/pttest.php "}

Clojure = > (resourcefully/post " http://ptchan.org/pttest.php" {} {: foo "bar": baz (FileInputStream. "/tmp/bak.jpg" )}) {: body-seq ( "Array" "(" "[foo] = > bar" "[baz] = > java.io.FileInputStream@320f6398" ")" ),: 200,: msg "OK",: "POST",: headers {: date ( "Fri, 02 Oct 2009 11:41:47 GMT" ),: ( "Accept-Encoding" ),: content-length ( "73" ),: connection (" close "),: content-type (" text/html "),: server (" Apache/2.2.9 (Debian) PHP/5.2.6-1 + lenny3 Suhosin-Patch "),: x-powered by by ( "PHP/5.2.6-1 + lenny3" )},: get-header #,: cookies nil,: url " http://ptchan.org/pttest.php" }

, . ? !

+5
2

clojure -apache-http, a Clojure Apache HTTP-. POST / .

+4

, clojure -http-client. , body, URL . , POST , - . , nu multipart.

(let [out (.getOutputStream connection)]
(cond
  (string? body) (spit out body)
  (map? body) (spit out (url-encode body))
  (instance? InputStream body)
  (let [bytes (make-array Byte/TYPE 1000)]
    (loop [#^InputStream stream body
           bytes-read (.read stream bytes)]
      (when (pos? bytes-read)
        (.write out bytes 0 bytes-read)
        (recur stream (.read stream bytes))))))
(.close out)))
+3
source

All Articles