ring

RAJKUMAR 2023-04-07T21:18:50.907099Z

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

RAJKUMAR 2023-04-07T21:19:00.238519Z

My project.clj is -

RAJKUMAR 2023-04-07T21:19:06.469759Z

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

RAJKUMAR 2023-04-07T21:19:13.488799Z

My handler is -

RAJKUMAR 2023-04-07T21:19:40.488239Z

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

seancorfield 2023-04-07T21:32:08.293329Z

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.

seancorfield 2023-04-07T21:36:34.537639Z

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

seancorfield 2023-04-07T21:40:29.840779Z

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.

seancorfield 2023-04-07T21:40:56.149039Z

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

RAJKUMAR 2023-04-07T21:19:57.162079Z

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

RAJKUMAR 2023-04-07T21:20:06.725179Z

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

RAJKUMAR 2023-04-07T21:20:16.584219Z

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

RAJKUMAR 2023-04-07T21:20:37.474689Z

RAJKUMAR 2023-04-07T21:20:44.405749Z

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

RAJKUMAR 2023-04-07T21:20:52.801049Z

The project is on github https://github.com/rajcspsg/clj-web-dev/tree/main/c02/ring-app

RAJKUMAR 2023-04-07T21:21:07.808359Z

asked the question in stackoverflow as well https://stackoverflow.com/questions/75961994/clojure-ring-http-is-not-giving-expected-response

seancorfield 2023-04-07T21:46:40.569789Z

I answered this question on SO as well...

RAJKUMAR 2023-04-08T18:41:26.486099Z

Thanks @seancorfield

RAJKUMAR 2023-04-08T18:41:35.497729Z

it worked 👍

👍🏻 1