Error "Not found" compojure.route

I have an update article route in a blog application.
It should submit a POST request with the updated post params then redirect to the newly updated post itself.

Instead it redirects to a “Not found” page where the URI including the article’s id is identical to the URI of the of the original article.

Steps taken

  1. Follow link to edit page:
    localhost:3000/article/4/edit

  2. Update post, and submit changes with save button.

  3. Browser redirects to
    http//localhost:3000/article/4

  4. “Not found” displayed instead of updated article

src/cljblog/handler.clj

(ns cljblog.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
            [ring.util.response :as resp]
            [cljblog.db :as db]
            [cljblog.pages :as p]))


(defroutes app-routes
  (GET "/" [] (p/index (db/list-articles)))

  (GET "/articles/new" [] (p/edit-article nil))

  (POST "/articles" [title body]
    (do (db/create-article title body)
        (resp/redirect "/")))

  (GET "/articles/:art-id" [art-id] (p/article (db/get-article-by-id art-id)))

  (GET "/articles/:art-id/edit" [art-id] (p/edit-article (db/get-article-by-id art-id)))

  (POST "/articles/:art-id/" [art-id title body]
    (do (db/update-article art-id title body)
        (resp/redirect (str "/articles/" art-id))))

  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))

src/cljblog/pages.clj


(ns cljblog.pages
  (:require [hiccup.page :refer [html5]]
            [hiccup.form :as form]
            [ring.util.anti-forgery :refer [anti-forgery-field]]))

(defn base-page [& body]
  (html5
   [:head [:title "CljBlog"]]
   [:hr]
   [:body
    [:a {:href "/"} [:h1 "CljBlog"]]
    [:a {:href "/articles/new"} "New article!"]
    body]))

(defn index [articles]
  (base-page
   (for [a articles]
     [:h2 [:a {:href (str "/articles/" (:_id a))} (:title a)]])))

(defn article [a]
  (base-page
   [:a {:href (str "/articles/" (:_id a) "/edit")} "edit"]
   [:hr]
   [:small (:created a)]
   [:h1 (:title a)]
   [:p (:body a)]))

(defn edit-article [a]
  (base-page
   (form/form-to
    [:post (if a
             (str "/articles/" (:_id a))
             "/articles")]

     (form/label "title" "Title")
     (form/text-field "title" (:title a))

     (form/label "body" "Body")
     (form/text-area "body" (:body a))

     (anti-forgery-field)

     (form/submit-button "Save!")))
 )

src/cljblog/db.clj

(ns cljblog.db
  (:require [monger.core :as mg]
            [monger.collection :as mc]
            [monger.operators :refer [$set]])
  (:import [org.bson.types ObjectId]))

(def db-connection-uri (or (System/getenv "CLJBLOG_MONGO_URL")
                           "mongodb://127.0.0.1/cljblog-test"))

(def articles-coll "articles")

(def db (-> db-connection-uri
            mg/connect-via-uri
            :db))

(defn create-article [title body]
  (mc/insert db articles-coll
             {:title title
              :body body
              :created (new java.util.Date)}))


(defn update-article [art-id title body]
  (mc/update-by-id db articles-coll (ObjectId. art-id)
                   {$set
             {:title title
              :body body}}))

(defn list-articles []
  (mc/find-maps db  articles-coll))

(defn get-article-by-id [art-id]
(mc/find-map-by-id db articles-coll (ObjectId. art-id))
  )

*redirects work as intended elsewhere in the app.

I left a comment on SO - seems like there’s a trailing slash inconsistency.

1 Like

Solution by @p-himik :

… your (POST "/articles/:art-id/" ...) route has a trailing slash, while the POST request itself doesn’t

https://stackoverflow.com/questions/72356776/error-not-found-clojure-compojure-route