gpt4 book ai didi

lisp - 如何在 POST 请求中发送文件?

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:09 24 4
gpt4 key购买 nike

我正在为我的网站构建一个 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] 似乎正在检查这个特定的类,但仍然得到了相同的结果。

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

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"}

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

谢谢

最佳答案

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

关于lisp - 如何在 POST 请求中发送文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com