Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send a file in a POST request?
    text
    copied!<p>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 <code>curl -F foo=bar baz=@bak.jpg foobar.com</code>.</p> <p>I'm using <a href="http://github.com/technomancy/clojure-http-client/tree/master/src/clojure/http/" rel="nofollow noreferrer">clojure-http-client</a>, and initially tried the form <code>(resourcefully/post "foobar.com" {} {:foo "bar" :baz (File. "bak.jpg")})</code>, but the :baz field was ignored by the receiving script, as if I had send only :foo. Later, I've tried changing <code>File.</code> to <code>FileInputStream</code>, as [the line 51][2] of client.clj seems to be checking for this particular class, but still got the same results.</p> <p>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:</p> <blockquote> <p>Clojure=> (resourcefully/post "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>" {} {: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 "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>"}</p> <p>Clojure=> (resourcefully/post "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>" {} {: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 "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>"}</p> <p>Clojure=> (resourcefully/post "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>" {} {: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 "<a href="http://ptchan.org/pttest.php" rel="nofollow noreferrer">http://ptchan.org/pttest.php</a>"}</p> </blockquote> <p>I'm not really sure how to proceed. Any suggestion? General tips on debugging are also welcome!</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload