/
json.clj
28 lines (26 loc) · 1011 Bytes
/
json.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns corbihttp.interceptor.json
(:require [byte-streams :as bs]
[cheshire.core :as json]
[exoscale.ex :as ex]))
(def json
{:name ::json
:leave (fn [ctx]
(if (coll? (get-in ctx [:response :body]))
(-> (update-in ctx [:response :body] json/generate-string)
(update-in [:response :headers] assoc "content-type"
"application/json"))
ctx))})
(def request-params
{:name ::json-params
:enter (fn [ctx]
(let [request (:request ctx)]
(if (:body request)
(try
(update-in ctx
[:request :body]
(fn [body]
(-> (bs/convert body String)
(json/parse-string true))))
(catch Exception _
(throw (ex/ex-incorrect "Fail to convert the request body to json"))))
ctx)))})