Access post json in clojure

EDIT . the source code is on github , if you're interested. Thanks


I'm a little confused on how to access the json data that was sent to the url in clojure; I just can't get it to work.

This is my route:

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

I'm not quite sure what I need to put in place request- I just saw a blog on the Internet and tried to follow it, but could not get it to work.

This is how I am trying to post: (from fiddler )

fiddler posting data

note: the request header port is different from the image; this is a mistake, I tried to spoil this data to see what it says, so ignore this part in the image above.

In curl, I just do this:

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

, clojure json, .

, var:

: {: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 #}

, params ...

compojure cheshire - json GET. , json clojure.

+5
3

, : params , " " .

ring-json . JSON : params . (https://github.com/ring-clojure/ring-json)

+6

, , . . 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)))

, .

+1

Just updating for previous answers is enough to add a key formatswith a list of formats that will be processed by the ring for your handler.

So something like

(def app (noir.util.middleware/app-handler 
           [your-routes]
           :formats [:json-kw]))
0
source

All Articles