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

The solution : post body input streams read twice (even more ) times #414

Closed xingzheone closed 5 years ago

xingzheone commented 5 years ago

Library Version(s)

https://stackoverflow.com/questions/9501237/read-stream-twice

Problem

374


(POST "/save_error_logger" [:as req]
    (let [input (:body req)
          restpositions (.reset input)]
      (log/info "type:" (type input))
      )
    (log/info "jieguo:" (slurp (:body req)))
    (ok "good"))
``

I don't think ```  wrap-format  (use :body-params) ``` is the right solution .  

 (.reset input)  is good solution  .   What about you?
Deraen commented 5 years ago

Using .reset is not that simple. Not all InputStream support mark and reset. Even if the used InputStream implementation supports these, .reset only moves the position of the stream to the position where it was when .mark was last called, and only if no more than readlimit bytes where read since mark was called.

xingzheone commented 5 years ago
(defn get-post-body-data [{:keys [body request-method] :as req}]
  (if (and (= :post request-method) body)
    (try
      (let [_ (.reset body)
            r (slurp body)
            _ (.reset body)]
        ;(assoc req :bodyp r)
        r)
      (catch Exception e
        (.printStackTrace e)
        )))
  )

my Monitor use this fn . and work fine