Liking cljdoc? Tell your friends :D

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:

  • Must be a map

  • Must contain a positive integer value for the :status

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. [1]

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.

KeyAlways Present?TypeDescription

: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).

1. Prior to Pedestal 0.7, an invalid :response would be ignored instead, and the execution would continue, subverting the developer’s expectations.

Can you improve this documentation? These fine people already did:
Howard M. Lewis Ship, Eric Dallo & Howard Lewis Ship
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close