Fork me on GitHub
#clojure
<
2016-02-21
>
gavin12:02:18

Hey simple question I think … I can’t seem to get my clojure ring application to decode JSON POST data into a proper map… help.

gavin12:02:25

Using

[ring/ring-core "1.3.0"]
                 [ring/ring-devel "1.3.0"]
                 [ring/ring-json "0.3.1"]
                 [ring/ring-jetty-adapter "1.3.0"]
                 [ring-basic-authentication "1.0.5"]
                 [compojure "1.1.8”] 

gavin12:02:34

[compojure.route :as route]
            [environ.core :refer [env]]
            [ring.adapter.jetty :as jetty]
            [ring.middleware.json :as json]
            [ring.middleware.keyword-params :as keyword-params]
            [ring.middleware.nested-params :as nested-params]
            [ring.middleware.params :as params]
            [ring.middleware.reload :refer :all]
            [ring.middleware.session :as session]
            [ring.middleware.session.cookie :as cookie]

gavin12:02:27

in main:

(jetty/run-jetty (-> #'app
                         (keyword-params/wrap-keyword-params)
                         (params/wrap-params)
                         (json/wrap-json-params)
                         (json/wrap-json-body)
                         (json/wrap-json-response)
                         (handler/site {:session {:store store}})

gavin12:02:47

😞 any help would be appreciated… or helpful redirects to a more suitable venue.

val_waeselynck12:02:54

@gavin: weed need more information on the failure and inspection of the incoming request

val_waeselynck12:02:05

(at least I do)

gavin12:02:16

one sec…

gavin12:02:14

when I send:

curl -s -X POST -H "Content-Type: application/json" -d '{"somekey" : "somevalue" }'  

gavin12:02:50

one moment…. tech difficulty…

gavin12:02:55

{:json-params {"somekey" "somevalue"},
 :ssl-client-cert nil,
 :cookies {},
 :remote-addr "0:0:0:0:0:0:0:1",
 :params {"somekey" "somevalue"},
 :flash nil,
 :headers
 {"accept" "*/*",
  "user-agent" "curl/7.30.0",
  "content-type" "application/json",
  "content-length" "25",
  "host" "localhost:5000"},
 :server-port 5000,
 :content-length 25,
 :form-params {},
 :session/key nil,
 :query-params {},
 :content-type "application/json",
 :character-encoding nil,
 :uri "/api/mesg",
 :server-name "localhost",
 :query-string nil,
 :body nil,
 :multipart-params {},
 :scheme :http,
 :request-method :post,
 :session {}}

gavin12:02:23

Note, the json params is not a proper map

jethroksy12:02:25

@gavin: instead of using wrap-json-body use wrap-json-params

jethroksy12:02:41

they look like alternatives

jethroksy12:02:56

wrap-json-body also accepts a map of options

jethroksy12:02:04

(def app
  (wrap-json-body handler {:keywords? true :bigdecimals? true}))

jethroksy12:02:31

in short, use one or the other?

gavin14:02:34

thanks for the info… gonna try it out!

danielsz20:02:55

For Clojure learners: an example web service leveraging boot, system, duct abstractions and components decorated with protocols has been updated to show maximum conciseness and elegance (although that is subjective): https://github.com/danielsz/system-dependency-injection