weavejester / compojure

A concise routing library for Ring/Clojure
Eclipse Public License 1.0
4.08k stars 259 forks source link

How to defined nested routes with `wrap-json-body` #173

Closed LukasRychtecky closed 6 years ago

LukasRychtecky commented 6 years ago

Hi, thank you for this library.

I run into an issue when having more routes wrapped into wrap-json-body.

(context "/transactions" []
  (wrap-json-body
    (POST "/" [:as request]
      ...)))

(context "/transactions/:id" [id]
  (wrap-json-body
    (PUT "/" [:as request]
      ...)))

Second route's body will be nil, because of InputStream (as it's pointed here https://github.com/weavejester/compojure/wiki/Common-Problems#a-middleware-tries-to-read-the-body-of-a-request-but-finds-it-empty).

How can I define these routes?

Thanks for an answer.

ghost commented 6 years ago

Why don't you wrap them together inside the json middleware only once?

(wrap-json-body
  (routes
     (context ...)
     (context ...)))
weavejester commented 6 years ago

As @lvbarbosa mentions, you can apply the JSON middleware only once. It will only consume the body if the content type is JSON, so should be safe either way.

Otherwise, you can use the compojure.core/wrap-routes function.

alehatsman commented 6 years ago

@LukasRychtecky if you get your answer, can we close issue?

LukasRychtecky commented 6 years ago

Yes, thank you for your help guys!