Search code examples
clojurepedestal

Getting the POST body data from a POST request to Pedestal


I have POSTed data to a Pedestal endpoint "/my-post. I have routed that end point as such:

[[["/" {:get landing} ^:interceptors [(body-params/body-params) ...]
  ["/my-post {:post mypost-handler}
  ....

So to my mind this means that the body-params interceptor will fire for /my-post too.

In mypost-handler I have:

(defn mypost-handler
   [request]
   ****HOW TO ACCESS THEN FORM DATA HERE ****
)      

How do I now access the form data here? I can see from printing the request that I have a #object[org.eclipse.jetty.sever.HttpInputOverHTTP..] which will clearly need further processing before it is useful to me.

(I must say, the documentation for Pedestal is pretty sketchy at best...)


Solution

  • Something like this should work. Note the body-params interceptor on the mypost-handler route

    (defn mypost-handler
      [{:keys [headers params json-params path-params] :as request}]
      ;; json-params is the posted json, so
      ;; (:name json-params) will be the value (i.e. John) of name property of the posted json {"name": "John"}
      ;; handle request 
      {:status 200
       :body "ok"})
    
    (defroutes routes
      [[["/mypost-handler" {:post mypost-handler}
         ^:interceptors [(body-params/body-params)]
         ]
        ]])