ring-clojure / ring-json

Ring middleware for handling JSON
313 stars 47 forks source link

Allowing body to be either string or stream #43

Closed ska2342 closed 8 years ago

ska2342 commented 8 years ago

When this middleware is used together with other middlewares it may run into a conflict trying to slurp the body. If the other middleware already slurped the body and put the resulting string back into the request the slurp in this middleware fails. This commit adds handling of string-type body.

weavejester commented 8 years ago

The request body can only be an InputStream, otherwise it's not a valid Ring request.

Ideally other middleware shouldn't consume the body if they don't need to. Checking the content-type of the request is a good way to avoid consuming request bodies that you don't need to. If you absolutely cannot avoid consuming the body (even after checking the content type!) then replace the body with a ByteArrayInputStream.

ska2342 commented 8 years ago

Oh, I wasn't aware that the body has to be an InputStream, probably I was so used to working with the Clojure hash-map, that the JSON middleware gives me.

I must consume all bytes of the body to validate it like this: https://developer.github.com/webhooks/securing/. Then I replaced the body like this:

(assoc request :body (io/input-stream (.getBytes body-string)))

but thought that'd be rather hackish. It seems that this is exactly what you suggest, so I am probably fine with it. I think we can close this PR then.. Thanks.