Reputation: 2850
In the below example, how can I set a default value for the path parameter item-id?
(POST "/:id" [item-id]
:path-params [item-id :- Int]
:body [body Body]
:query-params [{item-name :- Str nil}]
:summary "Create or update a item."
(ok ...))
Upvotes: 0
Views: 1064
Reputation: 1
The default value is used if no value is present in the URL for the parameter. Path parameters are made optional by appending a question mark (?) to the end of the parameter name. For example, id?. The difference between optional values and default route parameters is: A route parameter with a default value always produces a value. An optional parameter has a value only when a value is provided by the request URL. Path parameters may have constraints that must match the route value bound from the URL. Adding : and constraint name after the route parameter name
Upvotes: 0
Reputation: 268
You should match the path-parameter name to the string placeholder. Path-params don't need defaults - if there is no path-parameter present, the route doesn't match. Here's a working example:
(require '[compojure.api.sweet :refer :all])
(require '[ring.util.http-response :refer :all])
(require '[schema.core :as s])
(require '[muuntaja.core :as m])
(def app
(api
(POST "/:item-id" []
:path-params [item-id :- s/Int]
:query-params [{item-name :- s/Str nil}]
:summary "Create or update a item."
(ok {:item-id item-id
:item-name item-name}))))
(->> {:request-method :post
:uri "/123"
:query-params {"item-name" "kikka"}}
(app)
:body
(m/decode m/instance "application/json"))
; => {:item-name "kikka", :item-id 123}
Upvotes: 0