RJMetrics / sweet-liberty

A library for building database-backed RESTful services using Clojure
Apache License 2.0
104 stars 6 forks source link

Problem when posting JSON body #13

Open BernhardBln opened 9 years ago

BernhardBln commented 9 years ago

Hi,

I set up a sample project, by doing copy and paste from the sweet-liberty-example.

The GET works fine, now I'm trying to implement the POST.

It also works fine when I use URL-encoded form params in the body, but I prefer to send a JSON.

So I set the Content-Type to application/json, and put a json (which looks the same like the one I receive from the GET) in the body.

Then I get a stacktrace with an empty INSERT statement:

INSERT INTO CustomDocumentTemplateEntity ( ) VALUES ( )

Looking into the handlers.clj of sweet-liberty, I see this:

(defn make-create-entity-fn
  "Create a function that creates a new entity in storage and returns the new
  primary key value on the ctx at ::post-id."
  [table db-spec name-transforms input-transform _ conditions]
  (fn [{{form-params :form-params body-params :body-params} :request :as ctx}]
    (let [data (keywordize-keys (or body-params form-params))

So I assume for that to work, the data must be either in the :form-params or in the :body-params field of the request.

I'm using ring.middleware.json/wrap-json-body, which packs the data into :body (but not into :body-params), which in my opinion is correct.

Should the code maybe be like this instead? (Haven't tested it yet, only a suggestion)

  (fn [{ {:keys [form-params body-params body]} :request :as ctx}]
    (let [data (keywordize-keys (or body-params form-params body))

Or do I miss something in the setup that re-wraps the json into body-params? I made sure I added wrap-params, but don't think it would help here?