ring

Santiago 2019-12-31T10:48:29.001600Z

I'm using compojure for an API. All is fine except I can't get access to the original JSON payload from POST requests, which I need for verifying requests. Is this done by changing middlewear?

AJ Jaro 2019-12-31T15:52:14.003400Z

@slack.jcpsantiago Are you capturing the request argument to the route? For a GET macro, they should be in params and for a POST they should be in body

(GET "/basic/route/structure" request
  (let [params (:params request)]
    ...))

Santiago 2019-12-31T16:15:56.005200Z

@ajarosinski Yes, I'm doing that already. What I need is the raw json payload, not the parsed payload as a clojure structure

AJ Jaro 2019-12-31T16:21:41.006900Z

@slack.jcpsantiago Ah, hmm. That definitely is different than what I responded with, sorry. I don’t know exactly, but I have used the wrap-routes to add a middleware function and I think that still has the payload as a clojure structure. What are you trying to accomplish with the raw JSON instead of using the clojure structure?

Santiago 2019-12-31T16:31:01.008200Z

I need to verify a request from Slack. To do that I need to build a signature from the raw json plus a timestamp they send in the header

ikitommi 2019-12-31T16:46:04.011700Z

@slack.jcpsantiago you need to add a custom middleware that slurps the :body InputStream, places the slurped (raw) string under some key, e.g. :raw-body and either a) parses the body itself from JSON into :body-params, :json-params (or whatever your application expects) or b) re-creates the InputStream from the parsed body and let’s the normal body-parsing middleware read it.

ikitommi 2019-12-31T16:46:53.012500Z

(fn [handler]
  (fn [request]
    (let [raw-body (slurp (:body request))
          request' (-> request
                       (assoc :raw-body raw-body)
                       (assoc :body (ByteArrayInputStream. raw-body)))]
      (handler request'))))

ikitommi 2019-12-31T16:48:27.012900Z

something like that.

Santiago 2019-12-31T17:57:32.013300Z

@ikitommi thanks! I'll try that out