Search code examples
clojureclojure-core.logicpedestal

How does one simply write an interceptor that extracts json from a get/post request using Pedestal


I am making a simple API That will require to read body parameters from a json/edn request I am trying to get the program to echo the contents as edn objects but something seems to not work here is my routes

(def routes
  (route/expand-routes
   #{["/echo" :get [body-params/body-params print-response]  :route-name :greet]}))


The interceptor


(def print-response {:name ::print-response
                     :leave (fn [context]
                              (let [content-type (get-in context [:request :content-type])
                                    updated-response (assoc (-> context :response) :headers {"Content-Type" content-type}
                                                            :status  200
                                                            :body (get-in context [:request] :edn-params))]
                                (assoc context :response updated-response))
                              )
                     } 
                              
                             
)



Solution

  • fix : put parantheses around body-params explanation actually body-params is a higher order function that creates an interceptor so it must be called

    
    
    (def routes
      (route/expand-routes
       #{["/echo" :get [(body-params/body-params) print-response]  :route-name :greet]}))