我是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的" response”选项卡中是空的。需要我加点什么吗?顺便说一句,我使用的是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\""}))
))但没那么走运。
发布于 2018-03-20 17:41:18
对于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"))))本教程涵盖了liberator的基本概念:https://clojure-liberator.github.io/liberator/tutorial/
https://stackoverflow.com/questions/49369412
复制相似问题