无法在compojure中从POST请求中提取multipart-params

时间:2016-07-06 14:15:42

标签: clojure httpclient compojure ring noir

当我尝试从这样的POST请求中提取multipart-params时:

(defroutes upload-routes
  (POST "/upload" {params :params} (println params))

我得到{}

然后我试着这样:

(defroutes upload-routes
  (POST "/upload" {multipart-params :multipart-params} (println multipart-params))

我仍然有{}

我猜我的中间件有问题。

所以我尝试更改处理程序,这是我尝试过的处理程序:

(ns cloudserver.handler
  (:require [compojure.core :refer [defroutes routes]]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [cloudserver.routes.home :refer [home-routes]]
            [noir.util.middleware :as noir-middleware]
            [cloudserver.routes.auth :refer [auth-routes]]
            [cloudserver.routes.upload :refer [upload-routes]]
            [cloudserver.routes.search :refer [search-routes]]
            [cloudserver.routes.download :refer [download-routes]]
            [ring.middleware.defaults :refer [api-defaults wrap-defaults site-defaults]]
            [ring.middleware.multipart-params :refer [wrap-multipart-params]]
            [ring.middleware.params :refer [wrap-params]]
            [noir.session :as session]
            [ring.middleware.session.memory :refer [memory-store]]))


(def app
  (->
   (routes auth-routes
           home-routes
           upload-routes
           search-routes
           download-routes
           app-routes)
   session/wrap-noir-session
   (wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)
   wrap-multipart-params
   wrap-params))

(def app
  (->
   (routes auth-routes
           home-routes
           upload-routes
           search-routes
           download-routes
           app-routes)
   session/wrap-noir-session
   (wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)
   wrap-multipart-params))

(def app
  (->
   (routes auth-routes
           home-routes
           upload-routes
           search-routes
           download-routes
           app-routes)
   session/wrap-noir-session
   (wrap-defaults (-> site-defaults
                      (assoc-in [:security :anti-forgery] false)
                      (assoc-in [:params :multipart] true)
                      (assoc-in [:params :nested] true)))
   handler/site))

(def app
  (->
   (routes auth-routes
           home-routes
           upload-routes
           search-routes
           download-routes
           app-routes)
   wrap-multipart-params
   session/wrap-noir-session
   (wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)))

(def app
  (noir-middleware/app-handler
   [auth-routes
    home-routes
    upload-routes
    search-routes
    download-routes
    app-routes]
   :ring-defaults (assoc site-defaults :security nil)))

但我得到的唯一结果是{}

我的客户代码是:

public int upload (String filename, String[] tags, String time, String fingerprint) throws IOException {
    String url = host + "/upload";
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); 
    HttpPost httpPost =  new HttpPost(url);

    MultipartEntityBuilder mulentity = MultipartEntityBuilder.create();

    mulentity.addBinaryBody("photo", new File(filename));

    for (int i = 0; i < tags.length; i ++) {
        mulentity.addTextBody("tag" + i, tags[i]);
    }

    mulentity.addTextBody("fingerprint", fingerprint);
    mulentity.addTextBody("time", time);
    mulentity.addTextBody("filename", filename.substring(filename.lastIndexOf(File.separatorChar) + 1, filename.length()));

    HttpEntity entity = mulentity.build();
    httpPost.setEntity(entity);
    httpPost.setHeader("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);

    int status = 3;

    try {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpClient.execute(httpPost, responseHandler);
        status = Integer.parseInt(response);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } finally {
        httpClient.close();
    }
    return status;

}

我真的是clojure网络编程的绿手。非常感谢!

问题解决了。这是因为请求中的边界是错误的。

1 个答案:

答案 0 :(得分:2)

我也是&#34; Clojure Green&#34;,但在我的多部分Ring + Compojure API应用程序中,我只是根据多部分请求中的参数名称来构造请求。实际上是临时文件的那个由下面的printf输出有一个字符串表示,如&#34; #object [java.io.File 0x4blahblahblah&#34;。我认为,中间件负责将其从请求图中提取出来,并根据其名称提供参数。

(defroutes upload-routes
  (POST "/upload" [photo tag fingerprint time filename :as request]
    (printf
      "photo %s, tag %s, fingerprint %s, time %s, filename %s"
      photo tag fingerprint time filename)))

:如果您不打算对整个请求图执行任何操作,则不需要请求。如果您所做的只是printf ...也许将它包含在printf中,您将会看到香肠如何在Ring中制作。它的地图一路向下。