reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Joe 2020-11-19T13:00:48.350300Z

I have an API that uses Reitit and Swagger, with one of the routes being POST /book. The book object has a name, id, currency. When I use swagger to hit this end point, it works fine. But when I try to hit it with and http-kit/client POST, it gives me a coercion error. I think it has something to do with the request being turned into a Bytestream at some point, but if I put a slurp in my handler, presumably the swagger request will break (since presumably it's not a bytestream). What am I doing wrong here? This is the post request:

@(http/post "<http://localhost:3000/book>"
              {:body {:name "Jim LLC"
                      :id "021b148b-8d6a-425a-8cdb-9efad87b0d0d"
                      :currency :USD}})
This is the route, router config and handler
["/book" {:swagger {:tags ["Book"]}}
      ["" {:post {:handler create-book
                  :parameters {:body {:name string?
                                      :id uuid?
                                      :currency string?}}}}]]

(def router-config
  {:data {:coercion coercion-spec/coercion
          :muuntaja m/instance
          :middleware [swagger/swagger-feature
                       muuntaja/format-middleware
                       coercion/coerce-request-middleware]}})

(defn create-book [req]
  (tap&gt; req)
  (-&gt; req
      :body-params
      (qualify "book")
      (app/create-book!)
      (rr/response)))
And the (partial) response to the post is...

Joe 2020-11-19T13:01:40.350400Z

(with apologies for the awful formatting)

Joe 2020-11-19T13:06:22.350800Z

(I also tried doing a pr-str on the body map, and just writing it to JSON - no dice)

ikitommi 2020-11-19T14:14:19.351Z

try setting the Content-Type header in request and format the body into a JSON String.

ikitommi 2020-11-19T14:14:34.351200Z

now it says: :value nil and :content-type nil .

ikitommi 2020-11-19T14:15:46.351400Z

if no content-type is set in the request, the server doesn’t parse it and nothing get’s copied into parsed :value

Joe 2020-11-19T16:27:39.351600Z

Thanks Tommi, I got working with this

@(http/post "<http://localhost:3000/book>"
              {:headers {"content-type" "application/json"}
               :body (json/write-str {:name "Jim LLC"
                                      :id "121b148b-8d6a-425a-8cdb-9efad87b0d0d"
                                      :currency :USD})})

ikitommi 2020-11-19T18:04:27.351800Z

👍