reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Janne Sauvala 2021-05-09T19:39:52.247700Z

Hi, does my route config for a POST endpoint looks correct? I’m trying to set it up to accept form URL encoded parameters but it is not working

["/answer/submit" {:post {:parameters {:form [:map [:quiz [:int {:min 0 :max 1}]
                                                       :option [:int {:min 0 :max 1}]]]}
                             :responses  {200 {:body string?}
                                          404 {:body string?}
                                          400 {:body string?}}
                             :handler    answer-attempt}}]
(ring/router routes
                       {:data {:muuntaja   muuntaja/instance
                               :coercion   c-malli/coercion
                               :middleware [mw-muuntaja/format-middleware
                                            rrc/coerce-exceptions-middleware
                                            rrc/coerce-request-middleware
                                            rrc/coerce-response-middleware]}})

✅ 1
Janne Sauvala 2021-05-09T19:40:07.247800Z

With this setup I get error:

{
  "schema": "[:map {:closed true} [:quiz [:int {:min 0, :max 1}]]]",
  "errors": [
    {
      "in": [],
      "value": null,
      "message": "invalid type",
      "schema": "[:map {:closed true} [:quiz [:int {:min 0, :max 1}]]]",
      "path": [],
      "type": "malli.core/invalid-type"
    }
  ],
  "value": null,
  "type": "reitit.coercion/request-coercion",
  "coercion": "malli",
  "in": [
    "request",
    "form-params"
  ],
  "humanized": [
    "invalid type"
  ]
}

Janne Sauvala 2021-05-09T20:39:09.248200Z

Okay I got it working. I had a a few mistakes there. 1) missing reitit.ring.middleware.parameters/parameters-middleware from the middleware list 2) the :option parameter should be inside its own vector after :quiz:

[:map [:quiz [:int {:min 0 :max 10}]]
               [:option [:int {:min 0 :max 10}]]]