Returning values from a request in luminus

I am trying to learn luminus and I want to display the values of a request. I can display the keys by usiung the following code:

(defn ar []
  [ "" 
   {:middleware [middleware/wrap-csrf
                 middleware/wrap-formats]}
   ["/ar" {:get (fn [request] {:status 200 :body (keys request)})}]])

but if I want to display the values with following change I get an error:

(defn ar []
  [ "" 
   {:middleware [middleware/wrap-csrf
                 middleware/wrap-formats]}
   ["/ar" {:get (fn [request] {:status 200 :body (vals request)})}]])

No serializer found ....

vals and keys returns strings, so what is the problem? any suggestions?

vals and keys returns strings, so what is the problem? any suggestions?

vals and keys do not return strings, they both return a sequence of the actual keys and values in the request map.

The “No serializer found” message may be coming from trying to convert something in the request to some output (maybe JSON? Jackson seems to throw an error like that, but to be sure we’d need to see the actual error with a stacktrace).

true, they return keys and values. but if I put something like this, (vals (:1 “wfs”)) then it works, so it seems that many values like in the request require a serializer

For informal educational purposes, you can try (pr-str request) to make a string out of it. Most likely, there is at least one key with something horribly unprintable in it. Step 2 is to identify keys to exclude from the string, and replace request with (dissoc request :some-key) and so on.

1 Like

ok, pr-str works in the sense that it returns a file that I can inspect.
interestingly even a small text snippet like the return of (:uri request) ==> “/ar” requries a file download as answer.

“A file download” is probably for the best, as you did not HTML-encode the content. You could study the response headers (in browser developer tools or with a tool like curl) to figure out whether Luminus hinted that the browser should save a file, or the browser sniffed the content and came to that conclusion. However that might be, you would normally write this sort of stuff to a log file: logging to a file is more flexible and avoids interfering with the work the web server is supposed to be doing.

strangely it is named as an DMS file. even though it is 5 bytes still it needs do ask me if I want to open it in a text editor. and still, this works fine: (vals (:1 “wfs”)

ok, I solved it by passing vals request as a variable to the template:

(defn t3 []
  [ ""
     {:middleware [middleware/wrap-csrf
                 middleware/wrap-formats]}
   ["/t3" {:get
    (fn [request] (layout/render request "t3.html" {:name (vals request) :docs (-> "docs/t3.md" io/resource slurp)}))}]])

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.