我正在尝试使用 Liberator 实现一个集合资源,其中对集合 URL(例如 /posts
)的 POST
请求将创建一个新的博客文章项目。效果很好。不起作用的是使用 201 Created
响应来响应 POST
请求,其中包含指向新 URL 的 Location
header (例如 /posts/1
)。
我可以使用 201 Created
进行响应,但随后我无法包含 Location
header 响应,因此客户端不会知道新的 URL 是,或者我可以设置 :post-redirect? true
,并返回带有 Location
header 的 303 See Other
响应。
有没有办法从 Liberator POST 处理程序返回 201 Created
和 Location
header ?
最佳答案
每个处理程序都可以使用环响应返回包含 header 的完整环响应:
(defresource baz
:method-allowed? true
:new? true
:exists? true
:post! (fn [ctx] {::location "http://example.com"})
:post-redirect? false
:handle-created (fn [{l ::location }]
(ring-response {:headers {"Location" l}}))
关于clojure - 在 Liberator 中返回 201 Created 响应中的 Location header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14007366/