I'm building a clojure API to my website that is basically a wrapper around the original web API. One of the features that I'm not being able to implement is file sending via POST requests, basically what I would do in shell with curl -F foo=bar [email protected] foobar.com
.
I'm using 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 receiving script, as if I had send only :foo. Later, I've tried changing File.
to FileInputStream
, as [the line 51][2] of client.clj seems to be checking for this particular class, but still got the same results.
Then I created a php page that simply prints $_POST to check out my request, and apparently the objects' data is being sent literally. Take a look:
Clojure=> (resourcefully/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, 02 Oct 2009 11:41:15 GMT"), :vary ("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 ("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"), :vary ("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 ("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" ")"), :code 200, :msg "OK", :method "POST", :headers {:date ("Fri, 02 Oct 2009 11:41:47 GMT"), :vary ("Accept-Encoding"), :content-length ("73"), :connection ("close"), :content-type ("text/html"), :server ("Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny3 with Suhosin-Patch"), :x-powered-by ("PHP/5.2.6-1+lenny3")}, :get-header #, :cookies nil, :url "http://ptchan.org/pttest.php"}
I'm not really sure how to proceed. Any suggestion? General tips on debugging are also welcome!
Thanks
Try using clojure-apache-http, a Clojure wrapper for the full-featured Apache HTTP libraries. It does support multipart/form-data POST.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With