Fork me on GitHub
#ring
<
2023-04-07
>
RAJKUMAR21:04:50

Hi I'm trying out basic ring http application but not working as expected

RAJKUMAR21:04:00

My project.clj is -

RAJKUMAR21:04:06

(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)

RAJKUMAR21:04:13

My handler is -

RAJKUMAR21:04:40

(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}))

seancorfield21:04:08

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.

seancorfield21:04:34

Hmm, no, I can repro your issue without removing that...

seancorfield21:04:29

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.

seancorfield21:04:56

So your handler function was just returning a string, not a hash map.

RAJKUMAR21:04:57

When I do curl I get 200 response but not body and headers as set in my handler

RAJKUMAR21:04:06

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

RAJKUMAR21:04:16

My serverlog is printing the (println "got a request to process...") but not the response I'm expecting.

RAJKUMAR21:04:44

Any idea why I'm not getting body and response?

seancorfield21:04:40

I answered this question on SO as well...

RAJKUMAR18:04:35

it worked 👍

2