使用clj ajax对多部分/表单数据进行Http POST请求
我有一个端点,我可以上传一个带有curl的文本文件,如下所示:使用clj ajax对多部分/表单数据进行Http POST请求,ajax,clojure,clojurescript,Ajax,Clojure,Clojurescript,我有一个端点,我可以上传一个带有curl的文本文件,如下所示: curl -X POST -H "Content-Type: multipart/form-data" -F "file=@/resources/speciesDiffusion.tree" http://localhost:4000/continuous/tree 现在我需要从浏览器发送一个类似的请求,但是 (ajax/ajax-request {:uri (str "http://localhost:4000" "/c
curl -X POST -H "Content-Type: multipart/form-data" -F "file=@/resources/speciesDiffusion.tree" http://localhost:4000/continuous/tree
现在我需要从浏览器发送一个类似的请求,但是
(ajax/ajax-request
{:uri (str "http://localhost:4000" "/continuous/tree")
:method :post
:params {:treefile file}
:handler #(println %1)
:format (ajax/text-request-format)
:response-format (ajax/json-response-format {:keywords? true})})
给我一个(很好的json转换,所以我得到了那个部分,这很好)错误响应:
[false {:status 500, :status-text , :failure :error, :response {:timestamp 1494279686227, :status 500, :error Internal Server Error, :exception org.springframework.web.multipart.MultipartException, :message Current request is not a multipart request, :path /continuous/tree}}]
此外,在浏览器中,我可以看到内容类型标题设置不正确,但我无法将其与:format和:params的任何其他组合一起使用。在cljs ajax项目的自述文件中有一些示例。例如:
(let [form-data (doto
(js/FormData.)
(.append "id" "10")
(.append "file" js-file-value "filename.txt"))]
(POST "/send-file" {:body form-data
:response-format (raw-response-format)
:timeout 100}))
根据我的评论,问题不在于请求,而在于发送请求的功能,即我正在读取文件内容,而不是发送原始对象,如下所示:
(defn handle-file-upload [evt]
(let [target (.-currentTarget evt)
js-file (-> target .-files (aget 0))]
(do
(re-frame/dispatch [:post-file {:file js-file
:name (.-name js-file)}])
(set! (.-value target) ""))))
(defn upload-button []
(fn []
[:input {:class "none"
:id "file-upload"
:type "file"
:on-change #(handle-file-upload %)}]))
在哪里
:post文件
是一个调用执行POST请求的处理程序的事件。谢谢,但我很清楚现有文档,不幸的是,这并不能解决问题。我建议再次阅读,因为您的示例没有按照官方文档::params指定
:body
或任何:headers
,即将随请求一起发送的参数,格式相关::transit和:edn可以发送任何东西,:json和:raw需要提供一个映射。GET将把参数添加到查询字符串中,POST将把参数放在body.OK中。但是,正如我所演示的和文档所暗示的那样,多部分文章并非如此。问题实际上有点不同——我发送的是一个文件内容(通过js/FileReaders onLoad事件读取),而不仅仅是一个js文件。