I'm rather new to the whole topic and recently started playing around with ring. While writing some tests I've been surprised to find that "wrap-json-response" converts the body first into json and afterwards into a string, which I took some time to find it's intended behaviour but still can't wrap my head around why that's so.
--The wrap-json-response function generates a JSON string all in one go. I don't understand what you mean by "converts the body first into json and afterwards into a string". JSON is a way of formatting a data structure as a string of text.
If you're asking if wrap-json-response supports streaming, as of version 0.5.0 it does.
James Reeves
(ns test-rest-api.handler
(:require [ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.util.response :refer [response]]
[org.httpkit.server :refer [run-server]]))
(defn
handler [request]
(response {:foo "bar"}))
(defn -main [& args]
(let [handler (-> handler
(wrap-json-body)
(wrap-json-response))]
(run-server
handler {:port 8080})))
{:status 200
:headers {"Content-Type" "application/json"},
:body {"foo": "bar"}}
Instead the acutal response is:
{:status
200
:headers
{"Content-Type" "application/json"},
:body
"{\"foo\": \"bar\"}"}
Let's say I use something like this:
(ns test-rest-api.handler
(:require [ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.util.response :refer [response]]
[org.httpkit.server :refer [run-server]]))
(defn handler [request]
(response {:foo "bar"}))
(defn -main [& args]
(let [handler (-> handler
(wrap-json-body)
(wrap-json-response))]
(run-server handler {:port 8080})))
What I'd expect is something like:
Instead the acutal response is:
{:status 200
:headers {"Content-Type" "application/json"},
:body {"foo": "bar"}}
{:status 200
:headers {"Content-Type" "application/json"},
:body "{\"foo\": \"bar\"}"}
After looking it up I assumed it's intended, but why?