This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-07
Channels
- # babashka (31)
- # babashka-sci-dev (10)
- # beginners (64)
- # biff (11)
- # clerk (5)
- # cljdoc (2)
- # clojure (84)
- # clojure-boston (3)
- # clojure-conj (2)
- # clojure-europe (11)
- # data-science (19)
- # datomic (118)
- # fulcro (3)
- # graalvm (3)
- # hoplon (6)
- # inf-clojure (146)
- # instaparse (5)
- # lsp (13)
- # malli (3)
- # off-topic (13)
- # pedestal (5)
- # proletarian (5)
- # re-frame (14)
- # reitit (12)
- # releases (1)
- # ring (19)
- # scittle (2)
- # shadow-cljs (155)
- # slack-help (3)
(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)
(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.
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.
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 @U04V70XH6
So your handler function was just returning a string, not a hash map.