Skip to content
weavejester edited this page Jul 23, 2011 · 9 revisions

URL-encoded parameters are the primary way browsers pass values to web applications. They are send when a user submits a form, and are usually used for things like pagination.

Because Ring is a low level interface, it does not support parameters unless you apply the correct middleware:

ring.middleware.params/wrap-params

(wrap-params handler)
(wrap-params handler options)

This parses parameters from the query string in the URL, or from the HTTP request body, if the content type is set to application/x-www-form-urlencoded (which web browsers use to indicate submitted form data).

The function accepts a map of options. The recognized keys are:

  • :encoding The character encoding of the parameters. Defaults to the request character encoding, or "UTF-8" if no request character encoding is set.

The parsed parameters are encoded as maps, and placed in three keys in the request map passed to the handler:

  • :query-params A map of parameters from the query string
  • :form-params A map of parameters from submitted form data
  • :params A merged map of all parameters

Usually you'll only want to use the :params key, but the other keys are there if you happen to want them.

The parameters keys are always strings. The values are either strings, if there is only one value associated with the parameter name, or vectors, if there is more than one name/value pair with the same name.

ring.middleware.keyword-params/wrap-keyword-params

(wrap-keyword-params handler)

The base wrap-params middleware creates a map with parameter names encoded as strings. This is because URL-encoded parameter names can potentially include any character, and Clojure keywords cannot include characters like spaces or parenthesis.

However, if you only want to use parameter names with characters allowed in keywords, you can use the wrap-keyword-params middleware function to convert the parameter map keys into keywords.

This also works for nested parameter maps.

Clone this wiki locally