6

EDIT - The source code is on github if you're interested. Thanks


I am a bit confused as to how to access json data that has been posted to a url in clojure; I just can't seem to get it to work.

This is my route:

(cc/POST "/add" 
request
(str ": " request))

I am not entirely sure what I have to put in place of request - I just saw some blog online and tried to follow it, but couldn't get it to work.

Here's how I'm trying to post: (from fiddler)

fiddler posting data

note: request header port is different in the image; it's a mistake, I was trying to mess with that data to see what it says so ignore that part in the above image please

In curl, I'm just doing this:

curl -X POST -H "Content-Type: application/json" -d '{"foo":"bar","baz":5}' 
     http://localhost:3005/add

It looks like clojure is not receiving the json data that I posted at all.

Here's what the request var contains:

: {:scheme :http, :query-params {}, :form-params {}, :request-method :post, 
   :query-string nil, :route-params {}, :content-type "\"application/json\"", 
   :uri "/event", :server-name "localhost", :params {}, 
   :headers {"user-agent" "Fiddler", "content-type" "\"application/json\"", 
          "content-length" "23", "host" "localhost:3005"}, 
   :content-length 23, :server-port 3005, :character-encoding nil, :body #}

As you can see, all params are empty...

I am using compojure and cheshire - and I can convert data into json and return them just fine for GET routes.. I need to figure out how to pass json and convert it into clojure data..

thanks

4

3 回答 3

8

那是因为 :params 由处理“表单编码”主体的环形中间件填充。

您可以使用 ring-json 将您的应用程序包装到其他中间件中。它将解析 JSON 正文并相应地填充 :params。( https://github.com/ring-clojure/ring-json )

于 2013-02-05T15:08:10.133 回答
1

这是一个可以满足您要求的示例。代码取自这个项目。在 README 中,您将看到它支持的一些访问模式。代码有点乱,但它应该说明如何做到这一点。

(ns poky.protocol.http.jdbc.text
  (:require [poky.kv.core :as kv]
    (compojure [core :refer :all]
               [route :as route]
               [handler :as handler])
    [ring.util.response :refer [response not-found]]
    (ring.middleware [format-response :as format-response ]
                     [format-params :as format-params])
    [cheshire.core :as json]
    [ring.middleware.stacktrace :as trace]))

;
; curl -d"some data" -H'Content-Type: application/text' -v -X PUT http://localhost:8080/xxx
; curl -d'{"bla":"bar"}' -H'Content-Type: application/json' -v -X PUT http://localhost:8080/bla

(def valid-key-regex #"[\d\w-_.,]+")

; FIXME: this should be split- one fn for get, one for mget
(defn- wrap-get
  [kvstore ks params headers body]
  (response 
    (let [eks (clojure.string/split ks #",")
          nks (count eks)
          multi (> nks 1)
          ret (if multi (kv/mget* kvstore eks) (kv/get* kvstore ks))]
    (condp = (get headers "accept")
      "application/json" ret
      "text/plain" (if multi (throw (Exception. "Multi get unsupported with Accept: text/plain")) (get ret ks))
      ret))))



(defn- wrap-put
  [kvstore ks params headers body]
  (if (and 
        (= (get headers "content-type") "application/json")
        (get params (keyword ks) nil))
    (kv/set* kvstore ks (get params (keyword ks)))
    (kv/set* kvstore ks body))
  (response ""))

(defn api
  [kvstore]
  (let [api-routes
        (routes
          (GET ["/:ks" :ks valid-key-regex] {{:keys [ks] :as params} :params body :body headers :headers}
               (wrap-get kvstore ks params headers body))
          (PUT ["/:ks" :ks valid-key-regex] {{:keys [ks] :as params} :params 
                                             body :body body-params :body-params headers :headers} 
               (let [body (slurp body)
                     body (if (empty? body) body-params body)]
                 (wrap-put kvstore ks params headers body))))]

    (-> (handler/api api-routes)
        (format-params/wrap-format-params
          :predicate format-params/json-request?
          :decoder #(json/parse-string % true)
          :charset format-params/get-or-guess-charset)
        (format-response/wrap-format-response
          :predicate format-response/serializable?
          :encoders [(format-response/make-encoder json/encode "application/json")
                     (format-response/make-encoder identity "text/plain")]
          :charset "utf-8")
        trace/wrap-stacktrace)))

希望这可以帮助。

于 2013-04-11T20:36:22.367 回答
0

只是对先前答案的更新,现在足以将formats带有格式列表的键添加到您的处理程序中,这些格式将由 Ring 处理。

所以像

(def app (noir.util.middleware/app-handler 
           [your-routes]
           :formats [:json-kw]))
于 2016-12-14T10:02:29.650 回答