I am able to read RAW JSON of Post Request. But Not sure that I am doing it in right way or not?
CODE
(ns clojure-dauble-business-api.core
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[clojure-dauble-business-api.logic :as logic]
[clojure.tools.logging :as log]
[clojure-dauble-business-api.domain.artwork])
(:import [clojure_dauble_business_api.domain.artwork Artwork]))
(defapi app
(GET "/hello" []
(log/info "Function begins from here")
(ok {:artwork (logic/artwork-id 10)}))
(POST "/create" params
(log/info "Create - Function begins from here and body" (:name (:artwork (:params params))))
(ok {:artwork (logic/create-city (:name (:artwork (:params params))))})))
RAW JSON Of POST Request
{
"artwork": {
"id": 10,
"name": "DEFAULT"
}
}
using this line (:name (:artwork (:params params)))
to fetch "name" value from the above RAW Json.
If I am not doing in right way, Please guide me what will be the right way?
If I understand your question correctly, it looks like you are asking if there's a more "proper" way to fetch :name
with less awkward nesting of parentheses?
To retrieve a value such as :name
from a nested associative structure (hash-map) you can use get-in
:
(get-in params [:params :artwork :name])
This is neater and easier to read (left to right) with less nesting, but it is also a safer way to attempt to fetch a value, because get-in
will return nil
if it can't find a key in the sequence of keys.