Fork me on GitHub
#reitit
<
2021-09-26
>
az23:09:32

Hi, is there a way to automatically format transit? Using this fresh install

(defn service-routes []
  ["/api"
   {:coercion spec-coercion/coercion
    :muuntaja formats/instance
    :swagger {:id ::api}
    :middleware [;; query-params & form-params
                 parameters/parameters-middleware
                 ;; content-negotiation
                 muuntaja/format-negotiate-middleware
                 ;; encoding response body
                 muuntaja/format-response-middleware
                 ;; exception handling
                 coercion/coerce-exceptions-middleware
                 ;; decoding request body
                 muuntaja/format-request-middleware
                 ;; coercing response bodys
                 coercion/coerce-response-middleware
                 ;; coercing request parameters
                 coercion/coerce-request-middleware
                 ;; multipart
                 multipart/multipart-middleware]}
If I send a request to math/plus with a transit payload, I get a spec error. The body does not seem to be parsed. Any ideas?
["/plus"
     {:get {:summary "plus with spec query parameters"
            :parameters {:query {:x int?, :y int?}}
            :responses {200 {:body {:total pos-int?}}}
            :handler (fn [{{{:keys [x y]} :query} :parameters}]
                       {:status 200
                        :body {:total (+ x y)}})}
      :post {:summary "plus with spec body parameters"
             :parameters {:body {:x int?, :y int?}}
             :responses {200 {:body {:total pos-int?}}}
             :handler (fn [{{{:keys [x y]} :body} :parameters}]
                        {:status 200
                         :body {:total (+ x y)}})}}]]

Apple01:09:26

$ curl -X GET --header 'Accept: application/transit+json' ''
["^ ","~:total",9]
$

Apple01:09:44

no error here

az14:09:16

@UP82LQR9N I didn’t even try the get method. Are you able to use the post request? That’s where I’m stuck. Thank you

Apple15:09:42

$ curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"x": 1,"y": 3}' ''
{"total":4}
$

Apple15:09:36

i dont know how to post as transit-json in curl

az15:09:49

@UP82LQR9N - yeah json works fine, it’s only the transit encoding I’m having an issue with

az15:09:28

When I post via postman the transit payload doesn’t seem to parse and then it causes spec errors

az15:09:56

I wonder if most people are just using json and that’s why this isn’t much of an issue

Apple16:09:53

I connect to a CLJS repl and use this

Apple16:09:06

(POST "/api/plus" {:headers {"Accept" "Application/Transit+Json" "Content-Type" "Application/Transit+Json"} :params {:x 4 :y 6} :handler println :error-handler println})

Apple16:09:32

POST is from ajax.core

az16:09:20

and this worked? is it just params vs body?

Apple16:09:04

params vs body did you mean like params in path vs params in body? the screenshot shows it's in the body by POST

Apple16:09:20

the response is in json format though.

az17:09:03

@UP82LQR9N - Thank you. This was my exact request just using :body as the key instead of :params.

az17:09:26

(POST "" {:headers {"Accept" "Application/Transit+Json"} 
                                          :body {:quantity 2.9, :uuid "test uuid"}
                                          :handler tap>})

az17:09:05

It’s right there in the docs, I’m just a moron

az17:09:14

params and it works!

az17:09:34

@UP82LQR9N thanks for taking the time on this silliness

Apple17:09:10

(POST "/api/plus" {:headers {"Accept" "application/transit+json" "Content-Type" "application/transit+json"} :params {:x 4 :y 6} :handler println :error-handler println})
with this even the response back is in transit json format

az17:09:36

Yes, this is perfect