0
votes

I'm pretty new in the Clojure webdev ecosystem, I want to send a JSON response with the POST method using the liberator API, I tried this:

(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"))))

All looks fine, there is no error message, I get a "201 Created" response from ring, but the JSON data is not send, in Chrome "response" tab is just empty. Need I to add something? BTW, I'm using compojure, not compojure-api.

I also tried:

  (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\""}))
                                     ))

But no luck.

1
Could you check this link: clojure-liberator.github.io/liberator/tutorial/post-et-al.html also I could not see your post! functionErtuğrul Çetin
Since you're just starting out, now might be a good moment to compare the approaches of Ring, liberator and Yada: github.com/juxt/yada/blob/master/dev/resources/…Michiel Borkent

1 Answers

2
votes

For 201 Created responses you need to define the handler :handle-created, e.g.

(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"))))

The tutorial covers the fundamental concepts of liberator: https://clojure-liberator.github.io/liberator/tutorial/