Fork me on GitHub
#yada
<
2019-08-20
>
kirill.salykin09:08:55

Please advice, how I can have body read and parsed routes + handler

[""
                       [["/optimize"
                         (yada/resource
                          {:produces "application/json"
                           :consumes "application/json"
                           :methods  {:post {:response (fn [{:keys [body] :as ctx}]
                                                         (prn :B body)
                                                         ;; (prn :B (-> ctx (get-in [:request :body]) slurp))
                                                         (let [params (->> body
                                                                           (map (fn [[k v]]
                                                                                  [(str k) v]))
                                                                           (into {}))]
                                                           (core/optimize optimizer params)))}}})]]]
and tests
(let [routes (:routes http)
          body   (cheshire/generate-string {"key" {}})]
      (response-for routes :post "/optimize" {:headers {"Content-Type" "application/json"
                                                        "Accept"       "application/json"}
                                              :body    body)
      => {:status 200,
          :headers
          {"content-length" "2",
           "content-type"   "application/json"},
          :body   "OK"})

kirill.salykin09:08:50

currently body in hanlder is nil

kirill.salykin10:08:13

[:request :body] is :body (#object[java.nio.HeapByteBuffer 0x211c0729 "java.nio.HeapByteBuffer[pos=0 lim=14 cap=4096]"])

malcolmsparks10:08:44

Use byte-streams to convert to a string

kirill.salykin10:08:45

thanks, will give it a try

kirill.salykin10:08:54

the problem is - everything works as expected if I send via curl, but not working for response-for is there a way to have consistent behaviour?

malcolmsparks10:08:33

Aleph streams the HeapByteBuffer to the http browser, response-for bypasses aleph so the behaviour can't exactly match.

borkdude11:08:27

@kirill.salykin note that there is a with-aleph macro in the test code of yada that you could use

kirill.salykin11:08:05

is this replacement for response-for?

borkdude11:08:56

yes, I wrote it because of a similar problem

kirill.salykin12:08:08

awesome, thanks!

kirill.salykin12:08:08

do you have an example how it suppose to be used?

kirill.salykin12:08:54

what is resource in this case?

borkdude12:08:28

a yada resource

borkdude12:08:44

if you grep for with-aleph in the yada source, you'll find some examples

kirill.salykin12:08:50

just found it as well, thanks!