metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.11k stars 149 forks source link

disable middleware that "slurps" request body #328

Closed erjoalgo closed 7 years ago

erjoalgo commented 7 years ago

This is a question.

I'm porting my "vanilla" compojure routes into compojure-api routes (ie replacing [compojure.core :refer :all] with [compojure.api.sweet :refer :all]).

Those routes whose handlers try to read the raw request body, via (-> req :body slurp), now fail, since the body is empty. I imagine some middleware introduced by compojure.api.sweet has read the body earlier.

How do I get raw access to the data within my handlers? Can I disable any pre-processing of the request body?

Sample example below:


(ns bitstew-predix-connector.handler
  (:require
   ;; [compojure.core :refer :all]
   [compojure.route :as route]
   [clojure.data.json :as json]
   [clojure.tools.logging :as log]
   [ring.util.response :refer [response]]
   [ring.middleware.stacktrace :refer [wrap-stacktrace]]
   ;; [ring.swagger.ui :refer [swagger-ui]]
   [compojure.api.sweet :refer :all]
   [schema.core :as s]
   ))

(def app-routes
  (api
   {:swagger
    {:ui "/docs"
     :spec "/swagger.json"
     :data {:info {:title "api"
                   :description "api"}
            :tags [{:name "api", :description "some apis"}]}}}

   (GET "/" []
        :summary "redirect to swagger docs"
        (ring.util.response/redirect "/docs"))

   (context "/some/path" []

            (POST "/write"
                  []
                  :summary "write data into the service"
                  (fn [req]
                    (let [body (-> req :body slurp json/read-str)]
                      (log/debugf "received output data: %s\n" body)
                      "OK"
                      ))))))
ikitommi commented 7 years ago

Morning.

The docs of api describe all the options. Just to disable content negotiation, add :formats nil. There are also options to disable all mws.

In case of slurping JSON bodys, after content negotiation, the parsed parameters are in :body-params.

ikitommi commented 7 years ago

please reopen if this didn't help.

erjoalgo commented 7 years ago

Actually, this didn't work for me. I've tried both coercion: nil and :formats nil. I also don't see the parsed parameters in :body-params (I don't even have such a key). Do you have a short, complete example where coercion is disabled?

Saiedd commented 4 years ago

hi mate, do you even reach a solution? the problem that I used a middleware to wrap wrap-json-body which converts the body inputstream to a json, I don't know exactly how to stop slurp since I have now a json not inputstream