Fork me on GitHub
#yada
<
2017-06-29
>
bradford03:06:17

Hiii, why do I get a 204 No Content when I POST to try to create the phone number resource?

(defn post-phone [ctx]
  (phones/write-number-reported (:number (:parameters ctx)) (:country (:parameters ctx))))

(defn get-phone [ctx]
  (defn post-phone [ctx]
    (phones/read-number-reported (:number (:parameters ctx)))))

(defn server-routes []
  [""
   [
    ["/about" (yada/as-resource "Hello World")]
    ["/phone" :number (yada/resource
                       {:methods
                        {:get
                         {:parameters {:path {:number String}}
                          :produces   "application/json"
                          :response   get-phone}
                         :post {:parameters {:path {:number String}
                                             :body {:country String}}
                                :consumes   "application/json"
                                :produces   "application/json"
                                :response   post-phone}}})]]])
curl -X POST      -H 'cache-control: no-cache'   -H 'content-type: application/json'   -d '{"country":"us"}' -i

danielcompton03:06:24

What is post-phone returning?

bradford03:06:57

nil, it's a direct call to a dynamoDB fn.

bradford03:06:31

resources-are-data 🙏

bradford04:06:33

Hrm... I have it return a {} and I'm still getting a 204

bradford04:06:01

The debugger attached to the fn isn't executing, so I think my path is wrong?

danielcompton04:06:54

are your routes correct there?

bradford04:06:36

As far as I know, unless I have the format wrong...

danielcompton04:06:37

I would have expected something more like [["/phone" :number] (yada/resource ...)]

bradford04:06:28

Ahh. Thanks for the link! OK, I adjusted to that format, still getting a 204.

(defn server-routes []
  [""
   [
    ["/about" (yada/as-resource "Hello World")]
    ["/phone"
     [
      [["/" :number] (yada/resource
                       {:methods
                        {:get
                               {:parameters {:path {:number String}}
                                :produces   "application/json"
                                :response   get-phone}
                         :post {:parameters {:path {:number String}
                                             :body {:country String}}
                                :consumes   "application/json"
                                :produces   "application/json"
                                :response   post-phone}}})]]]]])

bradford04:06:21

I do like the route/resource composibility though, that's neat

danielcompton04:06:23

When you return {} Yada doesn't know if that is a resource, or a map

danielcompton04:06:47

you should instead get the response out of the context, add the {} to that, and return it

danielcompton04:06:15

(assoc-in ctx [:response :body] {})

danielcompton04:06:31

See yada.methods:250 for what's happening

danielcompton04:06:50

actually line 305

bradford23:06:22

I found out my problem, not sure how to fix it though. Everything resolves fine if I don't have an encoded URL, like ... however, if there's an encoded char in the URL, that's when I get the 204, like

bradford23:06:11

relevant resource:

(defn server-routes []
  [""
   [["/about" (yada/as-resource "Hello World")]
    ["/phone"
     [[["/" :number] (yada/resource
              {:parameters {:path {:number String}}
               :produces   ["application/json"]
               :methods
               {:get  {:response get-phone}
                :post {:parameters {:body {:country String}}
                       :consumes   "application/json"
                       :response   post-phone}}})]]]]])

bradford23:06:52

(defn post-phone [ctx]
  {})

(defn get-phone [ctx]
  {})