Srini
Srini

Reputation: 883

Compojure Rest API: Show body response in JSON format

I am new to compojure api.

How can I show body response in JSON format?

Here is the code what I have written

core.clj

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all])
  (:require [ring.util.http-response :refer :all])
  (:require [clojure-dauble-business-api.dbdev :as dbdev])
  (:require [yesql.core :refer [defquery]])
  (:require [cheshire.core :as json])
  (:require [ring.util.response :as response])
  (:require [clojure-dauble-business-api.logic :as logic])
  (:require [clojure.tools.logging :as log])
  (:require [clojure-dauble-business-api.artwork :as artwork]))

(defapi app
  (GET "/hello" []
   (log/info "Function begins from here"
    :return [artwork/artwork]
    :summary "Returns list of artworks"
    (response/response (logic/artworks)))))

Output of code is:

:return [{:id java.lang.Integer, #schema.core.OptionalKey{:k :name} java.lang.String}]
:summary Returns list of artworks
{:status 200, :headers {}, :body [{"id":25,"name":"Garden"},{"id":27,"name":"Lord Of the Rings Statue"},{"id":32,"name":"DEFAULT"},{"id":33,"name":"Garden"},{"id":39,"name":"garden"},{"id":83,"name":"yyeye"},{"id":86,"name":"DEFAULT"},{"id":88,"name":"wera"},{"id":137,"name":""},{"id":149,"name":"DEFAULT"}]}

How can I show :body of response into JSON format.

I need to show data in this way

 [
  {
    "id": 25,
    "name": "Garden"
  },
  {
    "id": 27,
    "name": "Lord Of the Rings Statue"
  },
  {
    "id": 32,
    "name": "DEFAULT"
  },
  {
    "id": 33,
    "name": "Garden"
  },
  {
    "id": 39,
    "name": "garden"
  },
  {
    "id": 83,
    "name": "yyeye"
  },
  {
    "id": 86,
    "name": "DEFAULT"
  },
  {
    "id": 88,
    "name": "wera"
  },
  {
    "id": 137,
    "name": ""
  },
  {
    "id": 149,
    "name": "DEFAULT"
  }
]

Upvotes: 1

Views: 419

Answers (1)

Srini
Srini

Reputation: 883

I got answer and figured out where I am doing wrong.

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all])
  (:require [ring.util.http-response :refer :all])
  (:require [clojure-dauble-business-api.dbdev :as dbdev])
  (:require [yesql.core :refer [defquery]])
  (:require [cheshire.core :as json])
  (:require [ring.util.response :as response])
  (:require [clojure-dauble-business-api.logic :as logic])
  (:require [clojure.tools.logging :as log])
  (:require [clojure-dauble-business-api.artwork :as artwork]))

(defapi app
  (GET "/hello" []
   (log/info "Function begins from here"
    :return [artwork/artwork]
    :summary "Returns list of artworks"
    (response/response (logic/artworks)))))

Should be

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all])
  (:require [ring.util.http-response :refer :all])
  (:require [clojure-dauble-business-api.dbdev :as dbdev])
  (:require [yesql.core :refer [defquery]])
  (:require [cheshire.core :as json])
  (:require [ring.util.response :as response])
  (:require [clojure-dauble-business-api.logic :as logic])
  (:require [clojure.tools.logging :as log])
  (:require [clojure-dauble-business-api.artwork :as artwork]))

(defapi app
  (GET "/hello" []
    (log/info "Function begins from here")
    :return [artwork/artwork]
    :summary "Return list of artworks"
    (ok (logic/artworks))))

Upvotes: 1

Related Questions