clojureswaggerplumatic-schemacompojure-api

:body-params vs :form-params in compojure-api


What's the difference between using :body-params and :form-params in practice when creating an API using compojure-api? For example:

(POST* "/register" []
    :body-params [username :- String,
                  password :- String]
    (ok)))

vs

(POST* "/register" []
    :form-params [username :- String,
                  password :- String]
    (ok)))

Which one should be used for an API that's going to be consumed by a single-page clojurescript app and/or native mobile apps?


Solution

  • It has to do with the supported content type:

    You'll want to use :body-params to consume it from your clojurescript or mobile apps.

    Using form-params or body-params will affect how the parameters are validated, from which part of the request they are taken, and how the swagger.json file would be generated if you use the swagger extensions.

    Many things in compojure-api are difficult to understand without mapping them to concepts in swagger. Looking into the code we find swagger terms like "consumes" or "formData" that can be found in the swagger parameter specs.