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> req)
(-> req
:body-params
(qualify "book")
(app/create-book!)
(rr/response)))
And the (partial) response to the post is...(with apologies for the awful formatting)
(I also tried doing a pr-str
on the body map, and just writing it to JSON - no dice)
try setting the Content-Type
header in request and format the body into a JSON String.
now it says: :value nil
and :content-type nil
.
if no content-type is set in the request, the server doesn’t parse it and nothing get’s copied into parsed :value
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})})
👍