yada

kirill.salykin 2019-08-20T09:58:55.034600Z

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.salykin 2019-08-20T09:59:50.035200Z

currently body in hanlder is nil

kirill.salykin 2019-08-20T10:02:13.035900Z

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

malcolmsparks 2019-08-20T10:02:44.036500Z

Use byte-streams to convert to a string

kirill.salykin 2019-08-20T10:03:45.037200Z

thanks, will give it a try

kirill.salykin 2019-08-20T10:09:54.038500Z

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?

malcolmsparks 2019-08-20T10:31:33.040300Z

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

kirill.salykin 2019-08-20T11:20:11.040600Z

clear, thanks!

borkdude 2019-08-20T11:57:27.041100Z

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

kirill.salykin 2019-08-20T11:59:05.041500Z

is this replacement for response-for?

borkdude 2019-08-20T11:59:56.041800Z

yes, I wrote it because of a similar problem

kirill.salykin 2019-08-20T12:00:08.042Z

awesome, thanks!

kirill.salykin 2019-08-20T12:14:08.042200Z

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

kirill.salykin 2019-08-20T12:14:54.042400Z

what is resource in this case?

borkdude 2019-08-20T12:16:28.042600Z

a yada resource

borkdude 2019-08-20T12:16:44.042800Z

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

kirill.salykin 2019-08-20T12:17:03.043Z

thanks

kirill.salykin 2019-08-20T12:19:50.043500Z

just found it as well, thanks!