Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Destructuring Syntax

James Reeves edited this page · 16 revisions

Compojure supports two sorts of destructuring:

  1. The Clojure kind, as one might use in the let special form, and
  2. A Compojure-specific kind designed for parsing parameters from the request map.

Regular Clojure Destructuring

The most general case is to capture the request map directly:

(GET "/" request
  (str request))

If you supply a map or symbol, Clojure's destructuring syntax will be used on the Ring request map. For example, here is how you'd bind a specific parameter to a variable using the Clojure syntax:

(GET "/:foo" {{foo :foo} :params}
  (str "Foo = " foo))

Compojure-specific Destructuring

Since regular destructuring can be quite verbose, Compojure offers a more specialised form of destructuring. If you supply a vector, Compojure will use this custom destructuring syntax. The above example can be written more concisely as:

(GET "/:foo" [foo]
  (str "Foo = " foo))

Compojure's parameter destructuring syntax has three pieces of functionality. The first is to bind parameters directly to symbols of the same name. For instance, assume we have the following request map:

{:request-method :get
 :uri "/foobar"
 :headers {}
 :params {:x "foo", :y "bar", :z "baz", :w "qux"}}

Then we can use a vector of symbols to bind each parameter:

[x y z]
x -> "foo"
y -> "bar"
z -> "baz"

To bind a map of all unassigned parameters, you can use the "&" symbol, followed by a variable name:

[x y & z]
x -> "foo"
y -> "bar"
z -> {:z "baz", :w "qux"}

This behaviour is analogous to the "&" as it is used in normal Clojure bindings. The difference is that instead of getting the unbound remainder of a list, we get the unbound remainder of a map.

Request binding

If you want to access other parts of the request map, you can use the :as keyword to assign the entire request map to a symbol:

[x y :as r]
x -> "foo"
y -> "bar"
r -> {:request-method :get
      :uri "/foobar"
      :headers {}
      :params {:x "foo", :y "bar", :z "baz", :w "qux"}}

You can also bind a Clojure destructuring map to the :as keyword:

[x y :as {u :uri}]
x -> "foo"
y -> "bar"
u -> "/foobar"}

Parameter coercion

As of Compojure 1.4.0, you can also supply coercion functions for parameters using the :<< keyword:

[x :<< as-int]

In the above case, the parameter x will be passed through the as-int function before being assigned. If any coercion function returns nil, then the coercion is considered to have failed, and the route will not match.

The compojure.coercions namespace contains two built-in coercions, as-int and as-uuid.

Something went wrong with that request. Please try again.