Question

I'm writing a Ring middleware, and also using Compojure. I want my middleware to look in the :params map to see if a specific key was provided by the user. In my middleware function, the request map does not contain a :params map, though. In the final request handler, there is a :params map. I'm thinking the :params map it not being set before my custom middleware, but I can't figure out how to get it to actually be set.

Any ideas?

(ns localshop.handler
  (:use [ring.middleware.format-response :only [wrap-restful-response]]
        [compojure.core])
  (:require [localshop.routes.api.items :as routes-api-items]
            [localshop.middleware.authorization :as authorization]
            [compojure.handler :as handler]))

;; map the route handlers
(defroutes app-routes
  (context "/api/item" [] routes-api-items/routes))

;; define the ring application
(def app
  (-> (handler/api app-routes)
      (authorization/require-access-token)
      (wrap-restful-response)))

Above is my handler.clj file, and below is the middleware itself.

(ns localshop.middleware.authorization)

(defn require-access-token [handler]
  (fn [request]
    (if (get-in request [:params :token])
      (handler request)
      {:status 403 :body "No access token provided"})))
Was it helpful?

Solution

I actually figured this out. This works if you adjust the (def app ... ) portion of the code so that it matches the following:

(def app
  (-> app-routes
      (wrap-restful-response)
      (authorization/require-access-token)
      (handler/api)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top