- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力从 put 返回 JSON!请求:
我的代码如下所示:
(defn body-as-string [ctx]
(if-let [body (get-in ctx [:request :body])]
(condp instance? body
java.lang.String body
(slurp (io/reader body)))))
(defn orbit-world [dimensions ctx]
(let [in (json/parse-string (body-as-string ctx))]
(json/generate-string in)))
(defn init-world [params]
(let [dimensions (Integer/parseInt params)
world (vec (repeat dimensions (vec (take dimensions (repeatedly #(rand-int 2))))))]
(json/generate-string world)))
(defresource world [dimensions]
:allowed-methods [:get :put]
:available-media-types ["application/json"]
:available-charsets ["utf-8"]
:handle-ok (fn [_] (init-world dimensions))
:put! (fn [ctx] (orbit-world dimensions ctx)))
我只是想将传递给 put 请求的任何内容作为 JSON 返回,直到我了解发生了什么。
但是如果我发出 put 请求,我会收到以下响应:
HTTP/1.1 201 Created
Date: Sun, 18 May 2014 15:35:32 GMT
Content-Type: text/plain
Content-Length: 0
Server: Jetty(7.6.8.v20121106)
我的 GET 请求返回 JSON,所以我不明白为什么 PUT 请求不是/
最佳答案
这是因为成功的 PUT 请求不会返回 http 200 状态代码(至少根据 liberator 的说法),它会返回 http 201 状态代码,正如您从响应中看到的那样。 Liberator 在不同的处理程序中分别处理 http 状态代码。为了实现你想要的,你必须这样做:
(defresource world [dimensions]
:allowed-methods [:get :put]
:available-media-types ["application/json"]
:available-charsets ["utf-8"]
:handle-ok (fn [_] (init-world dimensions))
:put! (fn [ctx] (orbit-world dimensions ctx))
:handle-created (fn [_] (init-world dimensions))) ; Basically just a handler like any other.
由于您在 :handle-created 上没有声明任何内容,因此它默认为具有文本/纯内容类型的空字符串。
编辑:
为了了解更多,您必须查看 decision graph 。在那里,您可以看到在处理 put!
后,它会进入决策处理 new?
,如果为 true,则进入 handle-created
如果为 false ,转到 respond-with-entity?
等等。
关于clojure liberator - 从 put 请求返回 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723785/
我已经根据 GotW 中显示的可悲的简单错误按放置新分配示例的想法实现了执行“就地对象回收”黑客攻击的功能#23 并在#28 中提到。 (是的,是的,我知道......但是这个 hack 实际上已经显
我想做的是 this ,但是如果我正在执行某个命令,并且该命令有输出,我也希望能够看到该命令的输出,所以,这就是我所做的: "javascript to hide statusbar noremap
几天来,使用 google-sites-liberation 备份 google 站点停止工作。 电话 java -cp google-sites-liberation.jar com.google.
我正在努力从 put 返回 JSON!请求: 我的代码如下所示: (defn body-as-string [ctx] (if-let [body (get-in ctx [:request :b
我在带有 XFCE4 桌面的 Debian 系统上使用 Iceweasel 浏览器(Firefox 衍生版)。 我没有在 ~/.config/fontconfig/fonts.conf 中定义任何自定
是Prismatic/schema很适合表单验证?我以前从未设计过表单验证库,但我想它会输出类似这样的东西而不是在 s/validate 上抛出异常: {::errors {:name [{:miss
我希望我的 POST 资源返回 200 OK 这样我就可以使用 :handle-ok 但资源返回 201 Created。 我将我的资源用于登录操作。 :handle-ok 不起作用! 这是示例代码:
如何将解放字体与我的 grails war 一起打包?这些字体可能在我要部署到的服务器上不可用,但我的报告系统需要这些字体。 如何将字体包含在 war 中,以便它们位于类路径上并可供 JVM 使用?
我正在尝试使用 Liberator 实现一个集合资源,其中对集合 URL(例如 /posts)的 POST 请求将创建一个新的博客文章项目。效果很好。不起作用的是使用 201 Created 响应来响
我是一名优秀的程序员,十分优秀!