Clojure, Compojure: Reading Post Request RAW Json -
i able read raw json of post request. not sure doing 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 here") (ok {:artwork (logic/artwork-id 10)})) (post "/create" params (log/info "create - function begins here , 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 line (:name (:artwork (:params params)))
fetch "name" value above raw json.
if not doing in right way, please guide me right way?
if understand question correctly, looks asking if there's more "proper" way fetch :name
less awkward nesting of parentheses?
to retrieve value such :name
nested associative structure (hash-map) can use get-in
:
(get-in params [:params :artwork :name])
this neater , easier read (left right) less nesting, safer way attempt fetch value, because get-in
return nil
if can't find key in sequence of keys.
Comments
Post a Comment