1

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?

Srini
  • 823
  • 5
  • 10
  • 29

4 Answers4

6

With Compojure 1.4.0 and above, you can coerce parameters with the syntax

[id :<< as-int]

Another option is to use Java interop to cast id to Integer:

(Integer/parseInt id)

Since you are using compojure-api, you can also coerce input and output data using Schema or spec.

Toni Vanhala
  • 1,352
  • 6
  • 16
  • Vahala I have posted answer to my question. Can you Please look into that and tell me If I am doing any thing wrong. – Srini Jul 25 '17 at 13:53
2

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))})))
Srini
  • 823
  • 5
  • 10
  • 29
  • 1
    Great, that is the first step. Going back and refining/making it more idiomatic, performance improvements can be done on subsequent passes IMHO. – Frank C. Jul 25 '17 at 20:43
1

You seem to be using compojure-api (https://github.com/metosin/compojure-api), which support both Schema and clojure.spec based coercion. Here is an example of path-parameter coercion with Schema: https://github.com/metosin/compojure-api/blob/master/examples/thingie/src/examples/pizza.clj#L62-L66

More details on the coercion here: http://www.metosin.fi/blog/clojure-spec-with-ring-and-swagger/

hope this helps.

Tommi

Tommi Reiman
  • 268
  • 1
  • 6
0

We leverage the Schema stuff to do this.

(context "" []
    :middleware [v2-exception-handler]
    :header-params [x-user-id :- X-User-Id]

where the X-User-Id schema is defined somewhere else like...

(s/defschema X-User-Id
  (rss/describe Long "ID of the user to operate on"))

I think all your validation is doing is verifying that the content of your string is all digits. Its not converting it to an Integer.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115