Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

: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?

like image 628
pupeno Avatar asked Aug 01 '15 22:08

pupeno


1 Answers

It has to do with the supported content type:

  • form-params are intended to be used with content-type "application/x-www-form-urlencoded" (a form)
  • body-params are intended to use for parameters which are also going to be located in the body with other content-types (edn, json, transit, etc).

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.

like image 155
nberger Avatar answered Nov 04 '22 05:11

nberger