clojure - 在 Liberator 中返回 201 Created 响应中的 Location header

标签 clojure liberator

我正在尝试使用 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/

相关文章:

clojure liberator - 从 put 请求返回 json

clojure - 多个 clojure-liberator 决策读取请求正文

unit-testing - 如何有效管理Clojure代码库?

Clojure 应用与映射

error-handling - 如何在 ClojureScript 错误消息中找到我自己的函数?

clojure - 如何从post返回json数据! Clojure 解放者中的处理程序?

clojure - Clojure 中的线程注释

clojure - 我自己的插入函数作为练习

clojure - 将 compojure noir 转换为 Liberator