-
Notifications
You must be signed in to change notification settings - Fork 1
Standard Library
The ring.util.response namespace contains functions for generating
responses. This can make your handlers more concise and
understandable.
In the last section the following example was used:
(defn what-is-my-ip [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body (:remote-addr request)})But by using the ring.util.response namespace, the function can be
written:
(defn what-is-my-ip [request]
(-> (response (:remote-addr request))
(content-type "text/plain")))TODO
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:
(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:
-
:encodingThe 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-paramsA map of parameters from the query string -
:form-paramsA map of parameters from submitted form data -
:paramsA 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.
(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.
URL-encoded parameters handle normal form data, but if you want to
upload files, you'll need multipart-encoded parameters. This
functionality can be added to your handler with the
wrap-multipart-params middleware.
(wrap-multipart-params handler)
(wrap-multipart-params handler options)The options for this middleware function are:
-
:encodingThe character encoding of the parameters. Acts the same as the same option inwrap-params. -
:storeA function to use to store the uploaded files. There are two stores included with Ring.
Multipart stores are covered in a later section.
TODO
TODO