/
json.clj
35 lines (32 loc) · 1.21 KB
/
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
29
30
31
32
33
34
35
(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))})
(defn json?
[request]
(= (-> request :headers (get "content-type"))
"application/json"))
(def request-params
{:name ::json-params
:enter (fn [ctx]
(let [request (:request ctx)]
(if (and (:body request)
(json? request))
(try
(update-in ctx
[:request :body]
(fn [body]
(-> (bs/convert body String)
(json/parse-string true))))
(catch Exception _
(throw (ex/ex-info "Fail to convert the request body to json"
[::invalid-json [:corbi/user ::ex/incorrect]]))))
ctx)))})