【问题标题】:How can I send a file in a POST request?如何在 POST 请求中发送文件?
【发布时间】:2009-10-02 11:45:53
【问题描述】:

我正在为我的网站构建一个 clojure API,它基本上是原始 Web API 的包装器。我无法实现的功能之一是通过 POST 请求发送文件,基本上我会在 shell 中使用 curl -F foo=bar baz=@bak.jpg foobar.com 执行此操作。

我使用的是clojure-http-client,最初尝试了(resourcefully/post "foobar.com" {} {:foo "bar" :baz (File. "bak.jpg")}) 的形式,但是接收脚本忽略了:baz 字段,就好像我只发送了:foo。后来,我尝试将File. 更改为FileInputStream,因为client.clj 的[第51 行][2] 似乎正在检查这个特定的类,但仍然得到相同的结果。

然后我创建了一个简单地打印 $_POST 以检查我的请求的 php 页面,显然对象的数据是按字面意思发送的。看看:

Clojure=> (足智多谋/发布“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=> (足智多谋/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=> (足智多谋/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", :方法“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"}

我不确定如何继续。有什么建议吗?也欢迎有关调试的一般提示!

谢谢

【问题讨论】:

    标签: lisp clojure


    【解决方案1】:

    尝试使用clojure-apache-http,它是全功能 Apache HTTP 库的 Clojure 包装器。它确实支持 multipart/form-data POST。

    【讨论】:

      【解决方案2】:

      我不确定是否可以使用 clojure-http-client。据我所见in the source code,如果您将地图作为主体参数传递,它将对每个元素进行 URL 编码并发送。看来您只能将文件作为整个主体发布,而无需任何其他参数。所以 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)))
      

      【讨论】:

        猜你喜欢
        • 2019-01-13
        • 2013-09-03
        • 1970-01-01
        • 2018-06-19
        • 2012-07-04
        • 2019-11-25
        • 2021-01-09
        • 2017-11-17
        • 2015-10-23
        相关资源
        最近更新 更多