ring

Marco Pas 2021-02-19T13:31:41.033300Z

I am trying to create my first ring/compojure application and seem to hit a wall. I am returning a simple JSON response and the app seems to always return with the incorrect content type.

(ns clojure-rest-server.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.util.response :refer [response]]
            [clojure.pprint]
            [ring.handler.dump :refer [handle-dump]]
            [ring.middleware.json :refer [wrap-json-response wrap-json-body]]
            [ring.middleware.defaults :refer [wrap-defaults api-defaults]]))

(defroutes app-routes
  (POST "/" {body :body} (response {:msg "hello-world"}))
  (route/not-found "Not Found"))

(def app
  (-> (wrap-defaults app-routes api-defaults)
      (wrap-json-body)
      (wrap-json-response)))
When i do a curl or use postman i allays get a
$ curl -i --header "Content-Type: application/json" \
  --request POST \
  <http://localhost:3000>

HTTP/1.1 200 OK
Date: Fri, 19 Feb 2021 13:30:50 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Server: Jetty(9.2.21.v20170120)

{"msg":"hello-world"}%
Any hint what i am doing wrong?

dharrigan 2021-02-19T14:01:17.033600Z

Oh I see you are here already

dharrigan 2021-02-19T14:01:23.033900Z

I hope my posting in #beginners helps

dharrigan 2021-02-19T14:34:30.034500Z

@marco.pasopas here's what I've done. it's quite simple. I am no means a user of compojure, but I hope it helps: https://github.com/dharrigan/ring-json

Marco Pas 2021-02-19T14:36:00.035100Z

@dharrigan Many thanks!! It seems to work now indeed with your help.

Marco Pas 2021-02-19T14:36:10.035400Z

Found an old issue that more or less dscribes this

Marco Pas 2021-02-19T14:36:14.035500Z

Marco Pas 2021-02-19T14:36:17.035900Z

https://github.com/ring-clojure/ring-defaults/issues/20

dharrigan 2021-02-19T14:36:31.036400Z

very good

Marco Pas 2021-02-19T14:37:33.037600Z

That whole middleware stuff is a bit confusing. Every demo on the internet more or less make the same mistake. The content type is not taken into account

Marco Pas 2021-02-19T14:37:56.037900Z

I should read the manual 🙂

dharrigan 2021-02-19T14:38:24.038500Z

I don't use compojure myself, I prefer #reitit (which uses ring underneath, as the's sorta the defacto standard)

Marco Pas 2021-02-19T15:16:17.039200Z

I am hit by the middleware from ring and not so much by compojure, retit will be my next thiing on the todo list 🙂