Hi I'm trying out basic ring http application but not working as expected
My project.clj is -
(defproject ring-app "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url ""
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url " "}
:dependencies [[org.clojure/clojure "1.11.1"]
[ring "1.10.0"]]
:repl-options {:init-ns ring-app.core}
:main ring-app.core) My handler is -
(ns ring-app.core
(:require [ring.adapter.jetty :as jetty]))
(defn handler [request-map]
(println "got a request to process ...")
{:status 200
:headers {"Content-Type" "text/html"}}
:body (str "<html><body> Your IP is : " (:remote-addr request-map) "</body></html>"))
(defn -main []
(println "starting handler...")
(jetty/run-jetty handler {:port 3000 :join? false}))If you have :join? false then -main is going to exit pretty much straight away and your web app will stop running.
Try removing that and see if you get the behavior you expect.
Hmm, no, I can repro your issue without removing that...
Ah, you have a typo in your handler function:
(defn handler [request-map]
(println "got a request to process ...")
{:status 200
:headers {"Content-Type" "text/html"}}
:body (str "<html><body> Your IP is : " (:remote-addr request-map) "</body></html>"))
should be
(defn handler [request-map]
(println "got a request to process ...")
{:status 200
:headers {"Content-Type" "text/html"}
:body (str "<html><body> Your IP is : " (:remote-addr request-map) "</body></html>")})
I noticed when I let my editor format your code -- and it became clear that the :body key and its value were outside the hash map.So your handler function was just returning a string, not a hash map.
When I do curl I get 200 response but not body and headers as set in my handler
curl -v
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.85.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Fri, 07 Apr 2023 21:10:29 GMT
< Content-Length: 0
< Server: Jetty(9.4.51.v20230217)
<
* Connection #0 to host localhost left intact My serverlog is printing the (println "got a request to process...") but not the response I'm expecting.
Any idea why I'm not getting body and response?
The project is on github https://github.com/rajcspsg/clj-web-dev/tree/main/c02/ring-app
asked the question in stackoverflow as well https://stackoverflow.com/questions/75961994/clojure-ring-http-is-not-giving-expected-response
I answered this question on SO as well...
Thanks @seancorfield
it worked 👍