This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-29
Channels
- # announcements (2)
- # babashka (18)
- # beginners (42)
- # calva (56)
- # chlorine-clover (35)
- # clara (9)
- # clj-http (1)
- # clj-kondo (19)
- # cljs-dev (8)
- # clojure (14)
- # clojure-europe (23)
- # clojure-france (7)
- # clojure-norway (6)
- # clojure-uk (3)
- # clojurescript (37)
- # community-development (3)
- # cursive (29)
- # datomic (3)
- # emacs (10)
- # events (2)
- # fulcro (77)
- # hyperfiddle (2)
- # introduce-yourself (4)
- # joker (1)
- # lsp (40)
- # malli (52)
- # meander (7)
- # missionary (16)
- # off-topic (3)
- # pedestal (1)
- # re-frame (5)
- # reitit (38)
- # releases (1)
- # shadow-cljs (4)
- # tools-build (18)
- # tools-deps (1)
- # xtdb (8)
I did not. 😛
I didn't seem to have a problem when I used the route on my setup, but let me delve further.
❯ http --verbose :8080/plain/plus x=10 y=20
POST /plain/plus HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 22
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/2.6.0
{
"x": "10",
"y": "20"
}
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json;charset=utf-8
{
"total": 30
}
change?
Null pointer exception. The body-params are nil by the time they reach the handler.
well, :body-params
in the request is nil
postman and curl. I thought it might be my POST. I thought it was interesting that you tried strings of integers instead of actual integers in your json, but that didn't work for me either.
(def janet-router
(ring/ring-handler
(ring/router
[["/plain"
["/plus" {:get (fn [{{:strs [x y]} :query-params :as req}]
{:status 200
:body {:total (+ (Long/parseLong x) (Long/parseLong y))}})
:post (fn blah [{{:keys [x y]} :body-params}]
{:status 200
:body {:total (+ (Long/parseLong x) (Long/parseLong y))}})}]]]
{:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:middleware [parameters/parameters-middleware
muuntaja/format-middleware
coercion/coerce-exceptions-middleware
coercion/coerce-request-middleware
coercion/coerce-response-middleware]}})
(ring/create-default-handler)))
since, no "spec" was defined for the input data, reitit won't coerece the data for you
so spec worked, that's nice. This also means the examples in the reitit repo are wrong, so I might be making a PR soon. While I have you, I'm going to try malli because I was using it before and also got stuck.
(def ::x int?)
(def ::y int?)
(def ::post-params (s/keys :req-un [::x ::y]))
(def janet-router
(ring/ring-handler
(ring/router
[["/plain"
["/plus" {:get (fn [{{:strs [x y]} :query-params :as req}]
{:status 200
:body {:total (+ (Long/parseLong x) (Long/parseLong y))}})
:post {:handler (fn blah [{{:keys [x y]} :body-params}]
{:status 200
:body {:total (+ x y)})
:parameters {:body ::post-params}})}]]]
{:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:middleware [parameters/parameters-middleware
muuntaja/format-middleware
coercion/coerce-exceptions-middleware
coercion/coerce-request-middleware
coercion/coerce-response-middleware]}})
(ring/create-default-handler)))
❯ http --verbose :8080/plain/plus x:=10 y:=20
POST /plain/plus HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 18
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/2.6.0
{
"x": 10,
"y": 20
}
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json;charset=utf-8
Date: Wed, 29 Dec 2021 14:29:33 GMT
Server: Jetty(9.4.42.v20210604)
{
"total": 30
}
have you ever tried in-router data specs?
(def janet-router
(ring/ring-handler
(ring/router
[["/plain"
["/plus" {:get (fn [{{:strs [x y]} :query-params :as req}]
{:status 200
:body {:total (+ (Long/parseLong x) (Long/parseLong y))}})
:post {:handler (fn blah [{{:keys [x y]} :body-params}]
{:status 200
:body {:total (+ x y)}})
:parameters {:body {:x int? :y int?}}}}]]]
{:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:middleware [parameters/parameters-middleware
muuntaja/format-middleware
coercion/coerce-exceptions-middleware
coercion/coerce-request-middleware
coercion/coerce-response-middleware]}})
(ring/create-default-handler)))
❯ http --verbose :8080/plain/plus x:=10 y=20
POST /plain/plus HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 20
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/2.6.0
{
"x": 10,
"y": "20"
}
HTTP/1.1 400 Bad Request
Content-Length: 348
Content-Type: application/json;charset=utf-8
Date: Wed, 29 Dec 2021 14:48:59 GMT
Server: Jetty(9.4.42.v20210604)
{
"coercion": "spec",
"in": [
"request",
"body-params"
],
"problems": [
{
"in": [
"y"
],
"path": [
"y"
],
"pred": "clojure.core/int?",
"val": "20",
"via": [
"spec$44271/y"
]
}
],
"spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$44271/x :spec$44271/y]), :type :map, :leaf? false})",
"type": "reitit.coercion/request-coercion",
"value": {
"x": 10,
"y": "20"
}
}
❯ http --verbose :8080/plain/plus x:=10 y:=20
POST /plain/plus HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 18
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/2.6.0
{
"x": 10,
"y": 20
}
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json;charset=utf-8
Date: Wed, 29 Dec 2021 14:49:58 GMT
Server: Jetty(9.4.42.v20210604)
{
"total": 30
}
I have the similar issue to @janetacarr, but In my case I'm trying to get :parameters, however, only when adding coercion middleware on the route it self, I get paramters.
{:summary "Plus with body-params"
:parameters {:body {:x int?, :y int?}}
:handler (fn [{{{:keys [x y]} :body} :parameters :as req}]
(spy (keys req)) ;; no parameters
{:status 200
:body {:total (+ x y)}})}