Response Map
The response map is attached to the context-map.adoc by any
interceptor in the interceptor chain. The response map describes the outgoing
HTTP response. If no response map is attached to the context by the time
processing is done, then Pedestal generates a 404 response.
When an interceptor attaches a response map (or a handler function returns a response map), the map is validated:
If the response fails the above validations, an exception is thrown and the request fails: the interceptor chain
switches to the :error stage, as for any other exception.
The exception message names the interceptor that attached the response and, once routing has matched a route,
the route name. It describes the invalid value by its type, or for a map, by its keys.
The usual cause is a function in a list of interceptors: Pedestal treats a function as a handler, so the value
it returns becomes the :response. A function meant to change the request must be an interceptor instead,
with an :enter function that returns the updated context:
;; Wrong: a handler; the request map it returns becomes the :response
(defn add-user [request]
(assoc request :user (current-user request)))
;; Right: an interceptor that updates the request in the context
(def add-user
{:name ::add-user
:enter (fn [context]
(update context :request #(assoc % :user (current-user %))))})
The function api:respond-with[] is the easiest way to add a response map to the interceptor context.
| Key | Always Present? | Type | Description |
|---|
:status | Yes | integer | The HTTP status code |
:headers | No | map of String → String | Response headers sent to the client. Header names are case-sensitive. |
:body | No | String, ISeq, File, InputStream, etc. | The body of the response sent to the client; see response-bodies.adoc for more details. |
|
|
Application code that returns a header should use the proper case version of the header, for example,
Content-Type (not content-type); although incoming request headers are converted
to lower case, outgoing response headers are left as-is, and some standard interceptors expects
certain headers to be present with the correct name (such as Content-Type).
|