clojurecompojurecompojure-api

Clojure, Compojure: Path Parameter always getting passed as String in Compojure API


I am passing path parameter to fetch data from Database.

End Point

http://localhost:3000/hello/1000

GET Method Code

Code

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [clojure-dauble-business-api.logic :as logic]
            [clojure.tools.logging :as log]
            [clojure-dauble-business-api.domain.artwork])
  (:import [clojure_dauble_business_api.domain.artwork Artwork]))

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+"] [id]
    (log/info "Function begins from here" id)
    (ok {:artwork (logic/artwork-id id)})))

When I hit the end Point from Postman I get this error,

org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying

I have come to know the value id is being passed as String value to query.

What is the best way to change Path paramter type to Number before passing to Query?


Solution

  • I have found one way to solve the issue, But Not Sure whether I am doing in correct way or not?

    (defapi app
      (GET ["/hello/:id", :id #"[0-9]+"] [id]
        (log/info "Function begins from here" id)
        (ok {:artwork (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong))})))