-
Notifications
You must be signed in to change notification settings - Fork 520
Creating responses
weavejester edited this page Aug 13, 2011
·
12 revisions
You can create Ring response maps manually (see Concepts), but the ring.util.response namespace contains a number of useful function to make this task easier.
The response
function creates a basic "200 OK" response:
(response "Hello World")
=> {:status 200
:headers {}
:body "Hello World"}
You can then use functions like content-type
to alter the base response to add in additional headers and other components:
(-> (response "Hello World")
(content-type "text/plain"))
=> {:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"}
Special functions also exist to create redirects:
(redirect "http://example.com")
=> {:status 302
:headers {"Location" "http://example.com"}
:body ""}
And to return static files or resources:
(file-response "readme.html" {:root "public"})
=> {:status 200
:headers {}
:body (io/file "public/readme.html")}
(resource-response "readme.html" {:root "public"})
=> {:status 200
:headers {}
:body (io/input-stream (io/resource "public/readme.html"))}
More information on these functions and others can be found in the ring.util.response API documentation.