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

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

在使用 compojure-api 创建 API 时,在实践中使用 :body-params 和 :form-params 有什么区别?例如:

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

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

哪个应该用于 API 将被单页 clojurescript 应用程序 and/or 本机移动应用程序使用?

与支持的内容类型有关:

  • form-params 旨在与内容类型 "application/x-www-form-urlencoded"(一种形式)
  • 一起使用
  • body-params 旨在用于参数,这些参数也将与其他内容类型(edn、json、transit 等)一起位于正文中。

您需要使用 :body-params 从您的 clojurescript 或移动应用程序中使用它。

使用 form-paramsbody-params 将影响参数的验证方式、从请求的哪一部分获取参数以及如果您使用 swagger.json 文件将如何生成招摇的扩展。

compojure-api 中的许多东西如果不将它们映射到 swagger 中的概念就很难理解。 Looking into the code we find swagger terms like "consumes" or "formData" that can be found in the swagger parameter specs.