This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-23
Channels
- # announcements (2)
- # atom-editor (3)
- # babashka (49)
- # beginners (100)
- # biff (9)
- # calva (78)
- # clj-kondo (18)
- # clojure (143)
- # clojure-europe (13)
- # clojure-germany (1)
- # clojure-nl (2)
- # clojure-spec (5)
- # clojure-sweden (2)
- # clojure-uk (4)
- # clojurescript (58)
- # conjure (1)
- # cursive (4)
- # datascript (11)
- # datomic (63)
- # docker (7)
- # emacs (18)
- # events (1)
- # fulcro (18)
- # graalvm (5)
- # helix (4)
- # improve-getting-started (13)
- # jobs (4)
- # jobs-discuss (3)
- # lsp (15)
- # malli (90)
- # membrane (14)
- # off-topic (12)
- # other-languages (5)
- # pedestal (7)
- # polylith (53)
- # re-frame (15)
- # reitit (23)
- # releases (4)
- # remote-jobs (9)
- # ring (11)
- # shadow-cljs (90)
- # specter (2)
- # testing (3)
- # tools-build (63)
- # vim (2)
- # xtdb (8)
I've set up a primitive ring server with the following routes:
(defn translate
[json]
"Translated JSON")
(defn process-translate
[req]
(println req)
(if-let [body (:body req)]
{:status 200 :headers {"Content-Type" "application/text" "Character-Encoding" "utf8"} :body (translate body)}
(default-malformed-response)))
(defroutes mercator-server
(GET "/" [] "mercator-server version 0.0.1")
(POST "/translate" req (process-translate req))
(POST "/translate/" req (process-translate req)))
When I pass in a well-formed JSON body, the request returns 200 and the expected body text. However, when I pass in a malformed JSON body, rather than returning the expected 400 status, I keep getting a 404 from the server. I'm using the wrap-json-body middleware, so a malformed json body should return a nil from (:body req), but somehow, ring seems to bounce the request back with a 404 before it even gets to the process-translate call. How do I get the handler to return a 400 status?Thanks for that, good to know. I've now also added the :malformed-response option to my wrap-json-body middleware and made sure the handler is NOT returning nil for a bad string:
(defn wrap-json-body [handler]
(fn [request]
(handler (json-body-request request {:keywords? true :malformed-response default-malformed-response}))))
(defn translate
[json]
"Translated JSON")
(defn process-translate
[req]
(println "In process-translate:" req)
(if-let [body (:body req)]
{:status 200 :headers {"Content-Type" "application/text" "Character-Encoding" "utf8"} :body (translate body)}
"Bad body"))
(defroutes mercator-server
(GET "/" [] "mercator-server version 0.0.1")
(POST "/translate" req (process-translate req))
(POST "/translate/" req (process-translate req)))
However, the middleware still seems to be returning a 404 status before even getting to the process-translate function.I've also read the code of wrap-json-body, and I shouldn't even have to add the :malformed-response default-malformed-response option, because the code supposedly defaults to returning that. But for some reason, it's returning a 404.
@U0DTU8J83 I'm not sure what is this wrap-json-body
in your code.
We are using this standard middleware like this: https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/ring/server.clj#L9-L12
(defn wrap [handler]
(-> handler
wrap-json-response
(wrap-json-body {:keywords? true})))
When you try to run this code it should work as expected:
curl -i -H 'Content-Type: application/json' -d '{a}'
HTTP/1.1 400 Bad Request
Date: Wed, 23 Feb 2022 15:16:58 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Server: Jetty(9.4.36.v20210114)
Malformed JSON in request body.%
curl -i -H 'Content-Type: application/json' -d '{}'
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 15:18:31 GMT
Content-Type: application/text
Character-Encoding: utf8
Transfer-Encoding: chunked
Server: Jetty(9.4.36.v20210114)
Translated JSON%
Doing it that way works. I have no idea why it works when the other code didn't, but thank you for that suggestion.
Hi I want to use ring's mock request to test a program/service/api So far it's coming along pretty well but I have a strange issue. I expect the result of the body to be a string, but
I end up getting
{:body #object[java.io.ByteArrayInputStream 0xhex... "java.io.ByteArrayInputStream@samehex"] ...}
Worth noting this is on Luminus with #muuntaja so I was thinking m/decode-whatever would work but it could not decode the ByteArrayInputStream. Am thinking Content-Type? But then it's application/edn on the request. So ... what to do.