Fork me on GitHub
#reitit
<
2021-05-09
>
Janne Sauvala19:05:52

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]}})

3
Janne Sauvala19:05:07

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 Sauvala20:05:09

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}]]]