Zuriar
Zuriar

Reputation: 11734

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

Upvotes: 4

Views: 2750

Answers (2)

Mark Melling
Mark Melling

Reputation: 1592

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)]
     ]
    ]])

Upvotes: 5

superkonduktr
superkonduktr

Reputation: 655

The mypost-handler is acting as a Ring handler, i. e. it should accept a Ring request map and return a Ring response map. Thus, you can expect a typical Ring request structure:

(defn mypost-handler
  [{:keys [headers params json-params path-params] :as request}]
  ;; handle request
  {:status 200
   :body "ok"})

Here's more relevant info on defining such handlers in your route tables.

Upvotes: 0

Related Questions