Fork me on GitHub
#pedestal
<
2020-09-08
>
emccue19:09:25

I am having some basic issues with interceptors

emccue19:09:47

I am trying to write an interceptor to just add some fields to the context object

emccue19:09:16

(defn respond-hello [db request]
  {:status 200 :body (str (request->str (jdbc/execute! db ["SELECT * FROM PERSON"]))
                          "\n"
                          (request->str request))})

(defn routes [db]
   (route/expand-routes
     #{["/greet" :get (partial respond-hello db) :route-name ::respond-hello]
       ["/" :get respond-elm-app :route-name ::spa]}))


(defn start-server
  [config db]
  (let [attach-system {:enter (fn [context]
                                (assoc context :context {:db db
                                                         :config config}))}]
    (-> {:env :dev
         ::http/routes (routes db)
         ::http/port (config/server-port config)
         ::http/type :jetty
         ::http/join? false}
        (http/default-interceptors)
        (update :io.pedestal.http/interceptors conj attach-system)
        http/create-server
        http/start)))

emccue19:09:26

[{:type java.lang.AssertionError
   :message "Assert failed: (every? interceptor/interceptor? interceptors)"
   :at [io.pedestal.interceptor.chain$enqueue invokeStatic "chain.clj" 273]}]

emccue19:09:29

but I get this

emccue19:09:43

which makes me think that somehow i structured my interceptor wrong

emccue19:09:24

but the error is the same regardless if I have a :name on the interceptor so i feel at a loss

isak20:09:14

What you inject there isn't an interceptor yet. You should call io.pedestal.interceptor/interceptor on that map before you inject it.

michaelteter21:09:28

Hello all. I'm making my first attempt to build an API, and I can't figure out how to get POSTed application/json data. My context in my route handler doesn't seem to have the request body json in it.

isak22:09:13

Did you look under :request in the context?

isak22:09:25

Are you using the io.pedestal.http.body-params/body-params interceptor? If so, you should look under [:request :io.pedestal.http.body-params/body-params]

michaelteter22:09:34

Thanks. I did finally get past the problem. This seems to give me what I need: `

["/add-event"   :post [(io.pedestal.http.body-params/body-params) pusher-api.incoming-data/add-event] :route-name :add-event]

3
michaelteter22:09:14

my mistake was not calling body-params ( ). I was just referencing it 😛