forked from ring-clojure/ring
-
Notifications
You must be signed in to change notification settings - Fork 1
创建响应(Responses)
YANG YUE edited this page Aug 9, 2016
·
1 revision
你可以手动创建Ring响应maps(查看一些概念),但是ring.util.response命名空间包含许多可以让这个任务简单的实用函数:
response
函数创建了一个基本的"200 OK"响应
(response "Hello World")
=> {:status 200
:headers {}
:body "Hello World"}
你可以使用像content-type
这样的函数去改变基础响应来添加一个额外的headers和其他组件:
(-> (response "Hello World")
(content-type "text/plain"))
=> {:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"}
特殊的函数来创建重定向:
(redirect "http://example.com")
=> {:status 302
:headers {"Location" "http://example.com"}
:body ""}
返回静态文件或者资源:
(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"))}
关于这些函数和其他函数的信息可以访问ring.util.response API documentation来获取.