Fork me on GitHub
#reitit
<
2020-11-19
>
Joe13:11:48

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 ""
              {: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> req)
  (-> req
      :body-params
      (qualify "book")
      (app/create-book!)
      (rr/response)))
And the (partial) response to the post is...

Joe13:11:40

(with apologies for the awful formatting)

Joe13:11:22

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

ikitommi14:11:19

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

ikitommi14:11:34

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

ikitommi14:11:46

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

Joe16:11:39

Thanks Tommi, I got working with this

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