ngrunwald / ring-middleware-format

Ring middleware for parsing parameters and emitting responses in JSON or other formats
163 stars 49 forks source link

Empty body causes "null" content from wrap-json-response #39

Closed joelittlejohn closed 9 years ago

joelittlejohn commented 9 years ago

When a response that has an empty body passes through wrap-json-response, the middleware parses a missing body to the json string "null" and produces a content length of 4 bytes.

app.web=> (use 'ring.middleware.format-response)
nil

app.web=> ((wrap-json-response identity) {:status 204})
{:headers {"Content-Length" "4", "Content-Type" "application/json; charset=utf-8"}, :body #<BufferedInputStream java.io.BufferedInputStream@7175bbbd>, :status 204}

app.web=> (-> ((wrap-json-response identity) {:status 204}) (update-in [:body] slurp))
{:headers {"Content-Length" "4", "Content-Type" "application/json; charset=utf-8"}, :body "null", :status 204}

I would expect the middleware to leave this response alone.

joelittlejohn commented 9 years ago

I'm using ring-json as an alternative now, but I thought I'd raise this to get some comments from elsewhere too.

hlship commented 9 years ago

I'm surprised that this was only reported 6 hours ago ... I've been meaning to report it for months.

I really think this just needs a little wrapper to ignore a nil body.

hlship commented 9 years ago

Created a PR: https://github.com/ngrunwald/ring-middleware-format/pull/40

ngrunwald commented 9 years ago

Closed by #40