首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clojure liberator库不发送JSON响应

Clojure liberator库不发送JSON响应
EN

Stack Overflow用户
提问于 2018-03-20 01:52:38
回答 1查看 150关注 0票数 0

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

代码语言:javascript
复制
(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的" response”选项卡中是空的。需要我加点什么吗?顺便说一句,我使用的是compojure,而不是compojure-api。

我也试过了:

代码语言:javascript
复制
  (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\""}))
                                     ))

但没那么走运。

EN

回答 1

Stack Overflow用户

发布于 2018-03-20 17:41:18

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

代码语言:javascript
复制
(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"))))

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49369412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档