【问题标题】:Clojure liberator library doesn't send the JSON responseClojure liberator 库不发送 JSON 响应
【发布时间】:2018-08-28 09:26:49
【问题描述】:

我是 Clojure webdev 生态系统的新手,我想使用 liberator API 使用 POST 方法发送 JSON 响应,我试过这个:

(POST   "/post/savecomment"
 request
 (resource
         :allowed-methods [:post]
         :available-media-types ["application/json"]
         :handle-ok (fn [ctx]
                      (format (str "{body: %s a: 1 b: 4}"), "the body part"))))

一切正常,没有错误消息,我从 ring 收到“201 Created”响应,但未发送 JSON 数据,Chrome 中的“响应”选项卡为空。需要我添加一些东西吗?顺便说一句,我使用的是 compojure,而不是 compojure-api。

我也试过了:

  (POST   "/post/savecomment" request  (resource
                                     :allowed-methods [:post]
                                     :available-media-types ["application/json"]
                                     :available-charsets ["utf-8"]
                                     :handle-ok (fn [_] (rep/ring-response {:status 200 :body "\"this is json\""}))
                                     :post! (fn [ctx] (rep/ring-response   {:status 666 :body "\"this is json\""}))
                                     ))

但没有运气。

【问题讨论】:

标签: clojure compojure liberator


【解决方案1】:

对于201 Created 响应,您需要定义处理程序:handle-created,例如

(POST   "/post/savecomment"
 request
 (resource
         :allowed-methods [:post]
         :available-media-types ["application/json"]
         :handle-created (fn [ctx]
                            (format (str "{body: %s a: 1 b: 4}"), "the body part"))))

教程涵盖了解放者的基本概念:https://clojure-liberator.github.io/liberator/tutorial/

【讨论】:

    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多